我知道有人问过类似的问题,并且我已尝试了所有建议解决此问题的方法,但我无法解决此问题。我正在制作一个文档,当按下一个单选按钮时,会出现另一个单选按钮,其选项会根据对第一个单选按钮的响应而有所不同。问题在于,选择第一个单选按钮后什么也没有发生,如果我理解正确,则变量设置为不等于该值。我什至摆脱了创建按钮并在其中添加辅助功能的循环。试图设置变量但没有运气的命令行...我开始认为这不能使用单选按钮完成...有人知道我是否缺少某些不允许设置变量的东西吗? / p>
from tkinter import Tk, Label, IntVar, Radiobutton
class pricingDoc:
def __init__(self, master):
self.master = master
master.title("Dyna-Vac Equipment Pricing GUI")
models = ["DJ5.636", "DJ1225", "DJ2040", "DJ4020", "DJ4030", "DJ6520"]
model_prices = [8139.00, 23770.00, 48050.00, 48040.00, 48030.00, 66050.0]
reels = ["""3/8" x 150'""", """1/2" x 300'""", """1/2" x 500'""", """3/4" x 600'""",
"""3/4" x 800'""", """1" x 600'""", """1" x 800'"""]
reel_prices = [0.00, 0.00, 850.00, 0.00, 1050.00, 0.00, 850.00]
self.label1 = Label(master, text="Jet Trailer Model")
self.label1.grid(row=1, column=1)
self.label2 = Label(master, text="Price, in USD")
self.label2.grid(row=1, column=3)
def showchoice(var1, updateint):
var1.set(updateint)
var1 = IntVar(master)
row_counter = 2
self.modelbutton = Radiobutton(master, text=models[0], variable=var1, value=0, indicatoron=False,
activebackground="green", width=12, command=lambda: showchoice(var1, 0))
self.modelbutton.grid(row=row_counter, column=1)
row_counter += 1
self.modelbutton = Radiobutton(master, text=models[1], variable=var1, value=1, indicatoron=False,
activebackground="green", width=12, command=lambda: showchoice(var1, 1))
self.modelbutton.grid(row=row_counter, column=1)
row_counter += 1
self.modelbutton = Radiobutton(master, text=models[2], variable=var1, value=2, indicatoron=False,
activebackground="green", width=12, command=lambda: showchoice(var1, 2))
self.modelbutton.grid(row=row_counter, column=1)
row_counter += 1
self.modelbutton = Radiobutton(master, text=models[3], variable=var1, value=3, indicatoron=False,
activebackground="green", width=12, command=lambda: showchoice(var1, 3))
self.modelbutton.grid(row=row_counter, column=1)
row_counter += 1
self.modelbutton = Radiobutton(master, text=models[4], variable=var1, value=4, indicatoron=False,
activebackground="green", width=12, command=lambda: showchoice(var1, 4))
self.modelbutton.grid(row=row_counter, column=1)
row_counter += 1
self.modelbutton = Radiobutton(master, text=models[5], variable=var1, value=5, indicatoron=False,
activebackground="green", width=12, command=lambda: showchoice(var1, 5))
self.modelbutton.grid(row=row_counter, column=1)
row_counter = 2
for price in model_prices:
self.label3 = Label(master, text=price)
self.label3.grid(row=row_counter, column=3)
row_counter += 1
self.label4 = Label(master, text="model {} is chosen".format(var1.get()))
self.label4.grid(row=row_counter, column=1)
row_counter += 1
self.label5 = Label(master, text="Reel Size Selection")
self.label5.grid(row=row_counter, column=1)
row_counter += 1
var2 = IntVar(master)
if var1.get() == 0:
self.reelbutton = Radiobutton(master, text=reels[0], variable=var2, value=0, indicatoron=False,
activebackground="green", width=12)
self.reelbutton.grid(row=row_counter, column=1)
row_counter += 1
row_counter = 10
self.label6 = Label(master, text=reel_prices[0])
self.label6.grid(row=row_counter, column=3)
elif 1 <= var1.get() <= 2:
self.reelbutton = Radiobutton(master, text=reels[1], variable=var2, value=1, indicatoron=False,
activebackground="green", width=12)
self.reelbutton.grid(row=row_counter, column=1)
self.label6 = Label(master, text=reel_prices[1])
self.label6.grid(row=row_counter, column=3)
row_counter += 1
self.reelbutton = Radiobutton(master, text=reels[2], variable=var2, value=2, indicatoron=False,
activebackground="green", width=12)
self.reelbutton.grid(row=row_counter, column=1)
self.label7 = Label(master, text=reel_prices[2])
self.label7.grid(row=row_counter, column=3)
row_counter += 1
elif 3 <= var1.get() <= 4:
self.reelbutton = Radiobutton(master, text=reels[3], variable=var2, value=3, indicatoron=False,
activebackground="green", width=12)
self.reelbutton.grid(row=row_counter, column=1)
self.label6 = Label(master, text=reel_prices[3])
self.label6.grid(row=row_counter, column=3)
row_counter += 1
self.reelbutton = Radiobutton(master, text=reels[4], variable=var2, value=4, indicatoron=False,
activebackground="green", width=12)
self.reelbutton.grid(row=row_counter, column=1)
self.label7 = Label(master, text=reel_prices[4])
self.label7.grid(row=row_counter, column=3)
row_counter += 1
elif var1.get() == 5:
self.reelbutton = Radiobutton(master, text=reels[5], variable=var2, value=5, indicatoron=False,
activebackground="green", width=12)
self.reelbutton.grid(row=row_counter, column=1)
self.label6 = Label(master, text=reel_prices[5])
self.label6.grid(row=row_counter, column=3)
row_counter += 1
self.reelbutton = Radiobutton(master, text=reels[6], variable=var2, value=6, indicatoron=False,
activebackground="green", width=12)
self.reelbutton.grid(row=row_counter, column=1)
self.label7 = Label(master, text=reel_prices[6])
self.label7.grid(row=row_counter, column=3)
row_counter += 1
row_counter += 1
self.label8 = Label(master, text="Total unit price as built: {}".format(model_prices[var1.get()] +
reel_prices[var2.get()]))
self.label8.grid(row=row_counter, column=3)
row_counter += 1
root = Tk()
my_gui = pricingDoc(root)
root.mainloop()
答案 0 :(得分:0)
根据我在代码中看到的和经过测试的结果,在我看来,省略号没有封装在单击单选按钮时调用的函数中。
为了检验这一理论,我在showchoice函数中添加了一个print(var1.get()),并在每次单击时打印出新索引。因此,为了更进一步,我也将label4创建行也移到了该函数中,并正确更新了表单。
在我看来,每次单击单选按钮时,似乎都需要将Elif块封装到一个被调用(并由此更新)的函数中。
我希望这可以帮助或至少使您走上正确的道路。我认为没有理由为什么不能或不应该将单选按钮用于您正在做的事情!
〜干杯!