我有一个for循环,旨在遍历列表,在 tkinter 中显示一些项目,等待按下按钮,然后存储一些Entry和Checkbutton数据。下面的代码是我正在尝试做的基础知识的 MRE。在下面的情况下,当Button被击中时,我想返回loop_function并从button_function.
我想也许使用类似lambda: continue或lambda: return可能将其带回第一个功能的东西,但那些会抛出错误。
有任何想法吗?
from tkinter import *
class TestClass(Frame):
def __init__(self, parent=None):
self.parent = parent
Frame.__init__(self)
self.main = self.master
self.f = Frame(self.parent)
self.f.pack()
(Button(self.f, text='Start',
command = self.loop_function)
.grid(column=0, row=0, padx=10, pady=10))
def loop_function(self):
name_list = ['Luke', 'Han', 'Leia', 'Chewie']
for n in name_list:
self.button_function(n)
force_user = self.fu.get()
side = self.sd.get()
print(n, force_user, side)
def button_function(self, n):
self.fu = IntVar(value=1)
self.sd = StringVar(value='rebel')
self.fu_e = Checkbutton(self.f, variable=self.fu)
self.sd_e = Entry(self.f, textvariable=self.sd)
col = 0
lbl_list = ['Name', 'Force User?', 'Side']
for l in lbl_list:
(Label(self.f, text=l, width=11, anchor=W)
.grid(column=col, row=0, padx=10, pady=10))
col += 1
(Label(self.f, text=n, width=11, anchor=W)
.grid(column=0, row=1, padx=5))
self.fu_e.grid(column=1, row=1)
self.sd_e.grid(column=2, row=1)
(Button(self.f, text='Save',
command = lambda: print('WAIT HERE!!'))
.grid(column=1, row=2, padx=10, pady=10))
if __name__ == '__main__':
root=Tk()
ui = TestClass(root)
ui.pack()
root.mainloop()
Qyouu
相关分类