我要在脚本中修复的部分将显示一个带有两个单选按钮和一个保存按钮的窗口。单击该按钮时,将显示一条消息,具体取决于是否选择了一个选项。
from tkinter import *
from tkinter import ttk
class MyApp(Tk):
def __init__(self):
Tk.__init__(self)
container = ttk.Frame(self)
container.pack(side="top", fill="both", expand = True)
self.frames = {}
for F in (PageOne, PageTwo):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky = NSEW)
self.show_frame(PageOne)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
def get_page(self, classname):
for page in self.frames.values():
if str(page.__class__.__name__) == classname:
return page
return None
PageOne类是我当前试图在Validate函数中修复的内容 如果选择并保存了单选按钮1,则我想打印少量分配;如果选择并保存了单选按钮2,则我想打印中等数量分配。
class PageOne(ttk.Frame):
def __init__(self, parent, controller):
ttk.Frame.__init__(self, parent)
ttk.Label(self, text='PageOne').grid(padx=(20,20), pady=(20,20))
self.controller = controller
self.make_widget(controller)
def make_widget(self, controller):
self.some_input = StringVar
self.some_entry = ttk.Entry(self, textvariable=self.some_input, width=8)
self.some_entry.grid()
self.v = StringVar()
self.v.set("b")
button1 = ttk.Button(self, text='Next Page',
command=lambda: controller.show_frame(PageTwo))
button1.grid()
Rad1 = ttk.Radiobutton(self, text="SMALL",variable=self.v, value="a")
Rad1.grid()
Rad2 = ttk.Radiobutton(self, text="MEDIUM",variable=self.v, value="b")
Rad2.grid()
button2 = ttk.Button(self, text="SAVE", command=validate)
button2.grid()
def validate(self):
value = self.v.get()
if value == "a":
print("Small Amount Dispensing")
elif value == "b":
print("Medium Amount Dispensing")
else:
print("An option must be selected")
app = MyApp()
app.title('Multi-Page Test App')
app.mainloop()
``````
答案 0 :(得分:0)
您必须在self.
中使用command=self.validate
button2 = ttk.Button(self, text="SAVE", command=self.validate)