【发布时间】:2018-10-02 18:26:48
【问题描述】:
我正在学习 GUI,作为测试,我正在尝试开发一个带有两个单选按钮的框架,一个用于随机模拟,一个用于文件输入模拟。当我想要文件输入模拟时,需要文件名,所以我的第二个单选按钮通过 switch_entry 命令打包新的标签/条目。如果第一个单选按钮被选中,调用的方法也应该删除标签和条目。
我尝试了 pack_forget 和销毁,但结果是我通过选择第二个单选按钮来创建 de 标签/条目,但不能通过选择第一个单选按钮来销毁它们。当我交替选择第一个和第二个单选按钮时,GUI 会不断添加标签/条目集。代码和图片如下:
import tkinter as tk
class InterRiver(tk.Frame):
def __init__(self, master = None):
self.mode = tk.StringVar(value="Random")
tk.Frame.__init__(self, master)
self.lblChooseMode = tk.Label(self, text="Opções do Simulador").pack(side="top")
self.rdModeRndm = tk.Radiobutton(self, text = "Rio Aleatório", value = "Random", variable = self.mode,
command = self.switch_entry).pack(anchor="w", side="top")
self.rdModeFile = tk.Radiobutton(self, text = "Rio a partir de arquivo", value= "File", variable = self.mode,
command = self.switch_entry).pack(anchor ="w", side="top")
self.btnStart= tk.Button(self, text = "Simular", command = simular).pack(side = "bottom")
self.lblCycles = tk.Label(self, text = "Número de Ciclos").pack(side="top")
self.ntyCycles = tk.Entry(self).pack(side="top")
def switch_entry(self):
lblFile = tk.Label(self, text="Insira o nome do arquivo")
ntyFile = tk.Entry(self)
if self.mode.get() == "File":
lblFile.pack(side="top")
ntyFile.pack(side="top")
else:
lblFile.destroy()
ntyFile.destroy()
if __name__ == "__main__":
w = tk.Tk()
InterRiver(w).pack()
w.title("Bears and Fishes")
w.mainloop()
【问题讨论】:
标签: python user-interface tkinter radio-button