Python 3.x 如何从不同的帧访问复选框值?
我正在创建一个tkinter应用程序,尝试从不同帧的多个复选框中获取值。当我在一个帧中单击复选框时,它也会在另一个帧中被选中。我想知道如何从不同的帧访问复选框值 当尝试选中复选框(框架1中的Tb1)时,它也在签入(NLframe中的NLTb1)。我想知道如何分别访问这两个复选框值Python 3.x 如何从不同的帧访问复选框值?,python-3.x,tkinter,Python 3.x,Tkinter,我正在创建一个tkinter应用程序,尝试从不同帧的多个复选框中获取值。当我在一个帧中单击复选框时,它也会在另一个帧中被选中。我想知道如何从不同的帧访问复选框值 当尝试选中复选框(框架1中的Tb1)时,它也在签入(NLframe中的NLTb1)。我想知道如何分别访问这两个复选框值 from tkinter import * def create_widgets_in_first_frame(): task_type=Button(main_frame,text='Task Type',c
from tkinter import *
def create_widgets_in_first_frame():
task_type=Button(main_frame,text='Task Type',command=call_second_frame_on_top).grid(row=6,column=0,sticky=W)
Network_Location=Button(main_frame,text='Network Location',command=call_third_frame_on_top).grid(row=7,column=0,sticky=W)
def create_widgets_in_second_frame():
T1=Label(frame1,text="Verify and ensure there is no duplicate entries present in task type",bg='Light blue')
T1.grid(row=3,columnspan=2,sticky=W)
#creating checkbutton
Tb1=Checkbutton(frame1,text='Pass',font=('Times New Roman',14),bg='Green')
Tb1.grid(row=3,column=4,padx=6)
#creating checkbuttonx
r2=Checkbutton(frame1,text='Fail',font=('Times New Roman',14),bg='red')
r2.grid(row=3,column=5,padx=6)
#creating Run button
b1=Button(frame1,text='Run').grid(row=3,column=6,padx=6)
button=Button(frame1,text='Go to Main page',command=call_first_frame_on_top)
button.grid(row=20,column=0,padx=6,sticky=W)
def create_widgets_in_third_frame():
NL1=Label(NLframe,text="Verify the migrated Network location in NGMSS and ensure all the mandatory information's are migrated along with it",bg='Light blue')
NL1.grid(row=3,columnspan=2,sticky=W)
#creating checkbutton
NLTb1=Checkbutton(NLframe,text='Pass',font=('Times New Roman',14),bg='Green')
NLTb1.grid(row=3,column=4,padx=6)
#creating checkbuttonx
nbr2=Checkbutton(NLframe,text='Fail',font=('Times New Roman',14),bg='red')
nbr2.grid(row=3,column=5,padx=6)
#creating Run button
nlb1=Button(NLframe,text='Run').grid(row=3,column=8)
button=Button(NLframe,text='Go to Main page',command= call_first_frame_on_top)
button.grid(row=20,column=0,padx=6,sticky=W)
def call_first_frame_on_top():
frame1.grid_forget()
NLframe.grid_forget()
main_frame.grid(row=0,column=0,sticky=W+E+N+S)
def call_second_frame_on_top():
NLframe.grid_forget()
main_frame.grid_forget()
frame1.grid(row=0,column=0,sticky=W+N+E+S)
def call_third_frame_on_top():
NLframe.grid(row=0,column=0,sticky=W+N+E+S)
frame1.grid_forget()
main_frame.grid_forget()
def quit_program():
root_window.destroy()
def raise_frame():
main_frame.tkraise()
root= Tk()
main_frame=Frame(root,height=30,width=500,borderwidth=10,bg='Powder Blue',highlightthickness=10,highlightcolor="red")
main_frame.grid(row=0,column=0,sticky=W+E+N+S)
frame1=Frame(root,height=30,width=500,borderwidth=10,bg='Powder Blue',highlightthickness=10,highlightcolor="red")
frame1.grid(row=0,column=0,sticky=W+N+E+S)
NLframe=Frame(root,height=30,width=500,borderwidth=10,bg='Powder Blue',highlightthickness=5,highlightcolor="red")
NLframe.grid(row=1,column=0,sticky=W+N+E+S)
create_widgets_in_third_frame()
create_widgets_in_second_frame()
create_widgets_in_first_frame()
frame1.grid_forget()
NLframe.grid_forget()
raise_frame()
root.mainloop()
`
首先在您的
退出程序
函数中,您调用要销毁的根窗口
。你以前从未定义过这个。我猜你是指root.destroy()
<强>第二版/强>,我想你可能想考虑使用单选按钮代替复选框。单选按钮只允许选中一个选项。这将阻止您在同一页上同时选中“通过”和“失败”框
就问题而言,您的复选框之所以在另一个框架中保持选中状态,是因为您只是忘记了网格,而没有破坏小部件。网格只是组织小部件的东西,而不是实际的小部件本身。因此,当您忘记网格并移动到新框架时,该网格点中的小部件将保持其状态。然而,这并不是说销毁小部件就是解决方案。要解决问题,只需确保将按钮的状态存储在变量中。我对你的代码做了一些调整,并使其正常工作from tkinter import *
def create_widgets_in_first_frame():
task_type = Button(main_frame, text='Task Type', command=call_second_frame_on_top).grid(row=6, column=0, sticky=W)
Network_Location = Button(main_frame, text='Network Location', command=call_third_frame_on_top).grid(row=7,
column=0,
sticky=W)
def create_widgets_in_second_frame():
t_var1 = BooleanVar()
t_var2 = BooleanVar()
T1 = Label(frame1, text="Verify and ensure there is no duplicate entries present in task type", bg='Light blue')
T1.grid(row=3, columnspan=2, sticky=W)
# creating checkbutton
Tb1 = Checkbutton(frame1, text='Pass', font=('Times New Roman', 14), bg='Green', variable=t_var1)
Tb1.grid(row=3, column=4, padx=6)
# creating checkbuttonx
r2 = Checkbutton(frame1, text='Fail', font=('Times New Roman', 14), bg='red', variable=t_var2)
r2.grid(row=3, column=5, padx=6)
# creating Run button
b1 = Button(frame1, text='Run').grid(row=3, column=6, padx=6)
button = Button(frame1, text='Go to Main page', command=call_first_frame_on_top)
button.grid(row=20, column=0, padx=6, sticky=W)
def create_widgets_in_third_frame():
nl_var1 = BooleanVar()
nl_var2 = BooleanVar()
NL1 = Label(NLframe,
text="Verify the migrated Network location in NGMSS and ensure all the mandatory information's are migrated along with it",
bg='Light blue')
NL1.grid(row=3, columnspan=2, sticky=W)
# creating checkbutton
NLTb1 = Checkbutton(NLframe, text='Pass', font=('Times New Roman', 14), bg='Green', variable=nl_var1)
NLTb1.grid(row=3, column=4, padx=6)
# creating checkbuttonx
nbr2 = Checkbutton(NLframe, text='Fail', font=('Times New Roman', 14), bg='red', variable=nl_var2)
nbr2.grid(row=3, column=5, padx=6)
# creating Run button
nlb1 = Button(NLframe, text='Run').grid(row=3, column=8)
button = Button(NLframe, text='Go to Main page', command=call_first_frame_on_top)
button.grid(row=20, column=0, padx=6, sticky=W)
def call_first_frame_on_top():
frame1.grid_forget()
NLframe.grid_forget()
main_frame.grid(row=0, column=0, sticky=W + E + N + S)
def call_second_frame_on_top():
NLframe.grid_forget()
main_frame.grid_forget()
frame1.grid(row=0, column=0, sticky=W + N + E + S)
def call_third_frame_on_top():
frame1.grid_forget()
main_frame.grid_forget()
NLframe.grid(row=0, column=0, sticky=W + N + E + S)
def quit_program():
root.destroy()
def raise_frame():
main_frame.tkraise()
root = Tk()
main_frame = Frame(root, height=30, width=500, borderwidth=10, bg='Powder Blue', highlightthickness=10,
highlightcolor="red")
main_frame.grid(row=0, column=0, sticky=W + E + N + S)
frame1 = Frame(root, height=30, width=500, borderwidth=10, bg='Powder Blue', highlightthickness=10,
highlightcolor="red")
frame1.grid(row=0, column=0, sticky=W + N + E + S)
NLframe = Frame(root, height=30, width=500, borderwidth=10, bg='Powder Blue', highlightthickness=5,
highlightcolor="red")
NLframe.grid(row=1, column=0, sticky=W + N + E + S)
create_widgets_in_third_frame()
create_widgets_in_second_frame()
create_widgets_in_first_frame()
frame1.grid_forget()
NLframe.grid_forget()
raise_frame()
root.mainloop()
旁注:以后请采取措施确保代码可读,并使用正确的命名约定。您可以首先找到,在
退出程序
函数中,调用要销毁的根窗口
。你以前从未定义过这个。我猜你是指root.destroy()
<强>第二版/强>,我想你可能想考虑使用单选按钮代替复选框。单选按钮只允许选中一个选项。这将阻止您在同一页上同时选中“通过”和“失败”框
就问题而言,您的复选框之所以在另一个框架中保持选中状态,是因为您只是忘记了网格,而没有破坏小部件。网格只是组织小部件的东西,而不是实际的小部件本身。因此,当您忘记网格并移动到新框架时,该网格点中的小部件将保持其状态。然而,这并不是说销毁小部件就是解决方案。要解决问题,只需确保将按钮的状态存储在变量中。我对你的代码做了一些调整,并使其正常工作from tkinter import *
def create_widgets_in_first_frame():
task_type = Button(main_frame, text='Task Type', command=call_second_frame_on_top).grid(row=6, column=0, sticky=W)
Network_Location = Button(main_frame, text='Network Location', command=call_third_frame_on_top).grid(row=7,
column=0,
sticky=W)
def create_widgets_in_second_frame():
t_var1 = BooleanVar()
t_var2 = BooleanVar()
T1 = Label(frame1, text="Verify and ensure there is no duplicate entries present in task type", bg='Light blue')
T1.grid(row=3, columnspan=2, sticky=W)
# creating checkbutton
Tb1 = Checkbutton(frame1, text='Pass', font=('Times New Roman', 14), bg='Green', variable=t_var1)
Tb1.grid(row=3, column=4, padx=6)
# creating checkbuttonx
r2 = Checkbutton(frame1, text='Fail', font=('Times New Roman', 14), bg='red', variable=t_var2)
r2.grid(row=3, column=5, padx=6)
# creating Run button
b1 = Button(frame1, text='Run').grid(row=3, column=6, padx=6)
button = Button(frame1, text='Go to Main page', command=call_first_frame_on_top)
button.grid(row=20, column=0, padx=6, sticky=W)
def create_widgets_in_third_frame():
nl_var1 = BooleanVar()
nl_var2 = BooleanVar()
NL1 = Label(NLframe,
text="Verify the migrated Network location in NGMSS and ensure all the mandatory information's are migrated along with it",
bg='Light blue')
NL1.grid(row=3, columnspan=2, sticky=W)
# creating checkbutton
NLTb1 = Checkbutton(NLframe, text='Pass', font=('Times New Roman', 14), bg='Green', variable=nl_var1)
NLTb1.grid(row=3, column=4, padx=6)
# creating checkbuttonx
nbr2 = Checkbutton(NLframe, text='Fail', font=('Times New Roman', 14), bg='red', variable=nl_var2)
nbr2.grid(row=3, column=5, padx=6)
# creating Run button
nlb1 = Button(NLframe, text='Run').grid(row=3, column=8)
button = Button(NLframe, text='Go to Main page', command=call_first_frame_on_top)
button.grid(row=20, column=0, padx=6, sticky=W)
def call_first_frame_on_top():
frame1.grid_forget()
NLframe.grid_forget()
main_frame.grid(row=0, column=0, sticky=W + E + N + S)
def call_second_frame_on_top():
NLframe.grid_forget()
main_frame.grid_forget()
frame1.grid(row=0, column=0, sticky=W + N + E + S)
def call_third_frame_on_top():
frame1.grid_forget()
main_frame.grid_forget()
NLframe.grid(row=0, column=0, sticky=W + N + E + S)
def quit_program():
root.destroy()
def raise_frame():
main_frame.tkraise()
root = Tk()
main_frame = Frame(root, height=30, width=500, borderwidth=10, bg='Powder Blue', highlightthickness=10,
highlightcolor="red")
main_frame.grid(row=0, column=0, sticky=W + E + N + S)
frame1 = Frame(root, height=30, width=500, borderwidth=10, bg='Powder Blue', highlightthickness=10,
highlightcolor="red")
frame1.grid(row=0, column=0, sticky=W + N + E + S)
NLframe = Frame(root, height=30, width=500, borderwidth=10, bg='Powder Blue', highlightthickness=5,
highlightcolor="red")
NLframe.grid(row=1, column=0, sticky=W + N + E + S)
create_widgets_in_third_frame()
create_widgets_in_second_frame()
create_widgets_in_first_frame()
frame1.grid_forget()
NLframe.grid_forget()
raise_frame()
root.mainloop()
旁注:以后请采取措施确保代码可读,并使用正确的命名约定。你可以找到