您好,我有一个程序,运行方式如下。当用户输入1-10之间的数字并点击GO按钮3时,checbox出现左,中,右。在仅选择1个复选框时,会出现一个按钮Next。选中1后禁用其他的。我想返回选择了复选框的主程序。我该怎么做呢。我可以通过“下一步”按钮或通过复选框选择来完成。我的代码在
之下import Tkinter as Tk
import ctypes
def setup_fc_error_message():
lines = ['fc is out of range: Please Enter a value from 1-10:']
MessageBox = ctypes.windll.user32.MessageBoxA
MessageBox(None, "\n".join(lines), 'Setup Info', 0)
def check_fc_range(fc_user):
# fc_float = si_units(fc_string_input)
# fc_float = float(fc_string_input)
fc_string_user = str(fc_user)
if (fc_user)>=1 and (fc_user<=10):
return (float(fc_user),fc_string_user)
else:
setup_fc_error_message()
return check_fc_range()
class MainWindow(Tk.Frame):
def __init__(self, parent):
Tk.Frame.__init__(self,parent)
self.parent = parent
self.parent.title('Test')
self.initialize()
def var_states_d(self):
return self.dvar
def var_states_r(self):
return self.rvar
def var_states_l(self):
return self.lvar
def cdb(self):
# print "variable is", self.dvar.get()
self.dvar_state = self.dvar.get()
# self.band_type = StringVar()
# self.band_type.set('D')
if self.dvar_state:
self.cr = Tk.Checkbutton(self.parent, text='Middle', state='disable', variable=self.rvar,command = self.crb)
self.cr.grid(row = 2, column = 1)
self.cl = Tk.Checkbutton(self.parent, text='Right', state='disable', variable=self.lvar,command = self.clb)
self.cl.grid(row = 2, column = 2)
self.nextbutton = Tk.Button(self.parent, text='NEXT', command= self.var_states_d)
self.nextbutton.grid(row=2,column=3)
else:
self.cr = Tk.Checkbutton(self.parent, text='Middle', variable=self.rvar,command = self.crb)
self.cr.grid(row = 2, column = 1)
self.cl = Tk.Checkbutton(self.parent, text='Right', variable=self.lvar,command = self.clb)
self.cl.grid(row = 2, column = 2)
self.nextbutton = Tk.Button(self.parent, text='NEXT', state='disable')
self.nextbutton.grid(row=2,column=3)
def crb(self):
# print "variable is", self.rvar.get()
self.rvar_state = self.rvar.get()
# self.band_type = StringVar()
# self.band_type.set('R')
if self.rvar_state:
self.cd = Tk.Checkbutton(self.parent, text='Left', state='disable', variable=self.dvar,command = self.cdb)
self.cd.grid(row = 2, column = 0)
self.cl = Tk.Checkbutton(self.parent, text='Right', state='disable', variable=self.lvar,command = self.clb)
self.cl.grid(row = 2, column = 2)
self.nextbutton = Tk.Button(self.parent, text='NEXT', command= self.var_states_r)
self.nextbutton.grid(row=2,column=3)
else:
self.cd = Tk.Checkbutton(self.parent, text='Left', variable=self.dvar,command = self.cdb)
self.cd.grid(row = 2, column = 0)
self.cl = Tk.Checkbutton(self.parent, text='Right', variable=self.lvar,command = self.clb)
self.cl.grid(row = 2, column = 2)
self.nextbutton = Tk.Button(self.parent, text='NEXT', state='disable')
self.nextbutton.grid(row=2,column=3)
def clb(self):
# print "variable is", self.lvar.get()
self.lvar_state = self.lvar.get()
# self.band_type = StringVar()
# self.band_type.set('L')
if self.lvar_state:
self.cd = Tk.Checkbutton(self.parent, text='Left', state='disable', variable=self.dvar,command = self.cdb)
self.cd.grid(row = 2, column = 0)
self.cr = Tk.Checkbutton(self.parent, text='Middle', state='disable', variable=self.rvar,command = self.crb)
self.cr.grid(row = 2, column = 1)
self.nextbutton = Tk.Button(self.parent, text='NEXT', command= self.var_states_l)
self.nextbutton.grid(row=2,column=3)
else:
self.cd = Tk.Checkbutton(self.parent, text='Left', variable=self.dvar,command = self.cdb)
self.cd.grid(row = 2, column = 0)
self.cr = Tk.Checkbutton(self.parent, text='Middle', variable=self.rvar,command = self.crb)
self.cr.grid(row = 2, column = 1)
self.nextbutton = Tk.Button(self.parent, text='NEXT', state='disable')
self.nextbutton.grid(row=2,column=3)
def initialize(self):
# self.frame = Tk.Frame(parent)
# self.frame.pack()
self.fc_gui = Tk.DoubleVar(self.parent)
self.fclabel1 = Tk.Label(self.parent, text = 'Please Enter a value between 1 and 10', fg = 'black', bg = 'yellow')
self.fclabel1.grid(row = 0, column = 0)
self.fcedit1 = Tk.Entry(self.parent, textvariable = self.fc_gui, bd = 5 )
self.fcedit1.grid(row = 1, column = 0)
self.fcbutton1 = Tk.Button(self.parent, text='GO', command = self.get_fc)
self.fcbutton1.grid(row = 1, column = 1)
def add_checkbox(self):
self.dvar = Tk.IntVar()
self.cd = Tk.Checkbutton(self.parent, text='Left', variable=self.dvar,command = self.cdb)
self.cd.grid(row = 2, column = 0)
self.rvar = Tk.IntVar()
self.cr = Tk.Checkbutton(self.parent, text='Middle', variable=self.rvar,command = self.crb)
self.cr.grid(row = 2, column = 1)
self.lvar = Tk.IntVar()
self.cl = Tk.Checkbutton(self.parent, text='Right', variable=self.lvar,command = self.clb)
self.cl.grid(row = 2, column = 2)
def get_fc(self):
self.fc_user = self.fc_gui.get()
if self.fc_user:
if check_fc_range(self.fc_user):
self.add_checkbox()
return self.fc_user
######################################################GUI portion ##############################################
def main():
root = Tk.Tk()
app = MainWindow(root)
fc_gui_user = app.get_fc()
app.mainloop()
if __name__ == "__main__":
main()
答案 0 :(得分:0)
我清理了您的MainWindow类,删除了cdb,crb,cld函数,因此每个被调用的on_toggle
只需要一个,以提高效率。您还会注意到add_checkbox
函数中的一些细微更改,使用lambda
传递特定检查按钮的值。因为我结合了这些函数,所以我对var_state
函数做了同样的事情。
我将Tk.IntVar
留在那里,因为你可能需要它们。虽然我不确定你想要用它们用于Checkbutton。
至于你的问题,你已经找到了一个找出选中了哪个复选框的方法,所以我不完全确定你所追求的是什么,但我已经概述了你可以检查它们的位置。
class MainWindow(Tk.Frame):
def __init__(self, parent):
Tk.Frame.__init__(self,parent)
self.parent = parent
self.parent.title('Test')
self.initialize()
self.chk_buttons = [] # List holding each checkbutton
self.var_states = [] # List holding each Invar for checkbutton
self.clicked = False # Variable to know if a checkbutton is currently selected
def get_var_states(self, i):
# Return IntVar of clicked checkbutton
return self.var_states[i]
def on_toggle(self, pos):
# print "variable is", self.dvar.get()
self.dvar_state = self.dvar.get()
# self.band_type = StringVar()
# self.band_type.set('D')
if not self.clicked:
# Changes all except clicked checkbox to disabled
# Altering state of existing widget
for i, chk_btn in enumerate(self.chk_buttons):
if i != pos:
chk_btn.configure(state='disable')
self.nextbutton = Tk.Button(self.parent, text='NEXT', command=lambda: self.get_var_states(pos))
self.nextbutton.grid(row=2,column=3)
# You can use these if statements to do something if a certain checkbutton is clicked
# Alternately you can call self.get_var_states(pos) here, instead of adding the command
# to the Next button so it runs when a checkbutton is clicked instead of when the Next button is clicked
if pos == 0: # Left
pass
if pos == 1: # Middle
pass
if pos == 2: # Right
pass
else:
# Changes all checkbox to normal
for chk_btn in self.chk_buttons:
chk_btn.configure(state='normal')
self.nextbutton = Tk.Button(self.parent, text='NEXT', state='disable')
self.nextbutton.grid(row=2,column=3)
self.clicked = not self.clicked #Changes between True and False with each click
def initialize(self):
# self.frame = Tk.Frame(parent)
# self.frame.pack()
self.fc_gui = Tk.DoubleVar(self.parent)
self.fclabel1 = Tk.Label(self.parent, text = 'Please Enter a value between 1 and 10', fg = 'black', bg = 'yellow')
self.fclabel1.grid(row = 0, column = 0)
self.fcedit1 = Tk.Entry(self.parent, textvariable = self.fc_gui, bd = 5 )
self.fcedit1.grid(row = 1, column = 0)
self.fcbutton1 = Tk.Button(self.parent, text='GO', command = self.get_fc)
self.fcbutton1.grid(row = 1, column = 1)
def add_checkbox(self):
# Added lambda function passing in a value corresponding to the index position in the list
self.dvar = Tk.IntVar()
self.cd = Tk.Checkbutton(self.parent, text='Left', variable=self.dvar,command = lambda: self.on_toggle(0))
self.cd.grid(row = 2, column = 0)
self.rvar = Tk.IntVar()
self.cr = Tk.Checkbutton(self.parent, text='Middle', variable=self.rvar,command = lambda: self.on_toggle(1))
self.cr.grid(row = 2, column = 1)
self.lvar = Tk.IntVar()
self.cl = Tk.Checkbutton(self.parent, text='Right', variable=self.lvar,command = lambda: self.on_toggle(2))
self.cl.grid(row = 2, column = 2)
self.chk_buttons = [self.cd, self.cr, self.cl] # List of checkboxes
self.var_states = [self.dvar, self.rvar, self.lvar] # List corresponding InvVar
def get_fc(self):
self.fc_user = self.fc_gui.get()
if self.fc_user:
if check_fc_range(self.fc_user):
self.add_checkbox()
return self.fc_user