python制作动态表格_python 动态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('', (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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值