python 動(dòng)態(tài)GUI表單生成器 腳本***

"""
##################################################################
a reusable form class, used by getfilegui (and others)
##################################################################
"""

from tkinter import *
entrysize = 40

class Form: # add non-modal form box
def init(self, labels, parent=None): # pass field labels list
labelsize = max(len(x) for x in labels) + 2
box = Frame(parent) # box has rows, buttons
box.pack(expand=YES, fill=X) # rows has row frames
rows = Frame(box, bd=2, relief=GROOVE) # go=button or return key
rows.pack(side=TOP, expand=YES, fill=X) # runs onSubmit method
self.content = {}
for label in labels:
row = Frame(rows)
row.pack(fill=X)
Label(row, text=label, width=labelsize).pack(side=LEFT)
entry = Entry(row, width=entrysize)
entry.pack(side=RIGHT, expand=YES, fill=X)
self.content[label] = entry
Button(box, text='Cancel', command=self.onCancel).pack(side=RIGHT)
Button(box, text='Submit', command=self.onSubmit).pack(side=RIGHT)
box.master.bind('<Return>', (lambda event: self.onSubmit()))

def onSubmit(self):                                      # override this
    for key in self.content:                             # user inputs in
        print(key, '\t=>\t', self.content[key].get())    # self.content[k]

def onCancel(self):                                      # override if need
    Tk().quit()                                          # default is exit

class DynamicForm(Form):
def init(self, labels=None):
labels = input('Enter field names: ').split()
Form.init(self, labels)
def onSubmit(self):
print('Field values...')
Form.onSubmit(self)
self.onCancel()

if name == 'main':
import sys
if len(sys.argv) == 1:
Form(['Name', 'Age', 'Job']) # precoded fields, stay after submit
else:
DynamicForm() # input fields, go away after submit
mainloop()

image.png

使用

from form import Form
from tkinter import Tk, mainloop
from tkinter.messagebox import showinfo
import getfile, os

class GetfileForm(Form):
def init(self, oneshot=False):
root = Tk()
root.title('getfilegui')
labels = ['Server Name', 'Port Number', 'File Name', 'Local Dir?']
Form.init(self, labels, root)
self.oneshot = oneshot

def onSubmit(self):
    Form.onSubmit(self)
    localdir   = self.content['Local Dir?'].get()
    portnumber = self.content['Port Number'].get()
    servername = self.content['Server Name'].get()
    filename   = self.content['File Name'].get()
    if localdir:
        os.chdir(localdir)
    portnumber = int(portnumber)
    getfile.client(servername, portnumber, filename)
    showinfo('getfilegui', 'Download complete')
    if self.oneshot: Tk().quit()  # else stay in last localdir

if name == 'main':
GetfileForm()
mainloop()

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • ------------------------------------------getfile.py !/us...
    SkTj閱讀 977評(píng)論 0 2
  • import sys, os, time, _thread as threadfrom socket import...
    SkTj閱讀 1,033評(píng)論 0 0
  • 目錄faster rcnn論文備注caffe代碼框架簡(jiǎn)介faster rcnn代碼分析后記 faster rcnn...
    db24cc閱讀 9,815評(píng)論 2 12
  • 洪二、釋其勝進(jìn)(分二科)荒一、徵【云何名為勝進(jìn)算數(shù)?】「勝進(jìn)算數(shù)」是怎麼回事情?這是「徵」。下面第二科是「辨」、說(shuō)...
    德虔閱讀 168評(píng)論 0 0

友情鏈接更多精彩內(nèi)容