前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python编程preview

python编程preview

作者头像
用户5760343
发布2022-05-13 17:19:48
3850
发布2022-05-13 17:19:48
举报
文章被收录于专栏:sktj

1、G.next()

2、pickle 用法

3、shelve

4、继承类的初始化写法:Obj.init

5 thinker

代码语言:javascript
复制
 from tkinter import *
 Label(text='Spam').pack()
 mainloop()
 6、thinker button
 from tkinter import *
 from tkinter.messagebox import showinfo
def reply():
 showinfo(title='popup', message='Button pressed!')
window = Tk()
 button = Button(window, text='press', command=reply)
 button.pack()
 window.mainloop()
 7、thinker Tk()/showinfo()/Entry()/Label()/Button()
 from tkinter import *
 from tkinter.messagebox import showinfo
def reply(name):
 showinfo(title='Reply', message='Hello %s!' % name)
top = Tk()
 top.title('Echo')

top.iconbitmap('py-blue-trans-out.ico')

代码语言:javascript
复制
Label(top, text="Enter your name:").pack(side=TOP)
 ent = Entry(top)
 ent.pack(side=TOP)
 btn = Button(top, text="Submit", command=(lambda: reply(ent.get())))
 btn.pack(side=LEFT)
top.mainloop()
 8、cgi::::::
 """
 Implement a web-based interface for viewing and updating class instances
 stored in a shelve; the shelve lives on server (same machine if localhost)
 """
import cgi, shelve, sys, os                   # cgi.test() dumps inputs
 shelvename = 'class-shelve'                   # shelve files are in cwd
 fieldnames = ('name', 'age', 'job', 'pay')
form = cgi.FieldStorage()                     # parse form data
 print('Content-type: text/html')              # hdr, blank line is in replyhtml
 sys.path.insert(0, os.getcwd())               # so this and pickler find person

main html template

代码语言:javascript
复制
replyhtml = """
 <html>
 <title>People Input Form</title>
 <body>
 <form method=POST action="peoplecgi.py">
 <table>
 <tr><th>key<td><input type=text name=key value="%(key)s">
 
代码语言:javascript
复制

 </table>
 <p>
 <input type=submit value="Fetch",  name=action>
 <input type=submit value="Update", name=action>
 </form>
 </body></html>
 """

insert html for data rows at

代码语言:javascript
复制
rowhtml  = '<tr><th>%s<td><input type=text name=%s value="%%(%s)s">\n'
 rowshtml = ''
 for fieldname in fieldnames:
 rowshtml += (rowhtml % ((fieldname,) * 3))
 replyhtml = replyhtml.replace('
代码语言:javascript
复制
', rowshtml)
def htmlize(adict):
 new = adict.copy()
 for field in fieldnames:                       # values may have &, >, etc.
 value = new[field]                         # display as code: quoted
 new[field] = cgi.escape(repr(value))       # html-escape special chars
 return new
def fetchRecord(db, form):
 try:
 key = form['key'].value
 record = db[key]
 fields = record.dict                   # use attribute dict
 fields['key'] = key                        # to fill reply string
 except:
 fields = dict.fromkeys(fieldnames, '?')
 fields['key'] = 'Missing or invalid key!'
 return fields
def updateRecord(db, form):
 if not 'key' in form:
 fields = dict.fromkeys(fieldnames, '?')
 fields['key'] = 'Missing key input!'
 else:
 key = form['key'].value
 if key in db:
 record = db[key]                       # update existing record
 else:
 from person import Person              # make/store new one for key
 record = Person(name='?', age='?')     # eval: strings must be quoted
 for field in fieldnames:
 setattr(record, field, eval(form[field].value))
 db[key] = record
 fields = record.dict
 fields['key'] = key
 return fields
db = shelve.open(shelvename)
 action = form['action'].value if 'action' in form else None
 if action == 'Fetch':
 fields = fetchRecord(db, form)
 elif action == 'Update':
 fields = updateRecord(db, form)
 else:
 fields = dict.fromkeys(fieldnames, '?')        # bad submit button value
 fields['key'] = 'Missing or invalid action!'
 db.close()
 print(replyhtml % htmlize(fields))                 # fill reply from dict
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • top.iconbitmap('py-blue-trans-out.ico')
  • main html template
  • insert html for data rows at
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档