Я пытаюсь напечатать текущее значение радиокнопки в структурированном приложении.Я видел несколько решений о том, как написать рабочий код (например, https://www.tutorialspoint.com/python/tk_radiobutton.htm), но когда я пытаюсь использовать его в своих классах, я делаю что-то не так.
У меня есть следующий фонкод:
class OutFileGUI(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, ".Out GUI")
container = tk.Frame(self)
container.pack(side='top', fill='both', expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
menubar = tk.Menu(container)
filemenu = tk.Menu(menubar, tearoff=0)
filemenu.add_command(label="Read file", command=self.open_file)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=quit)
menubar.add_cascade(label="File", menu=filemenu)
tk.Tk.config(self, menu=menubar)
self.frames = {}
for F in (StartPage, MainPage, ConvBehaviour, GaussPoints):
frame = F(parent=container, controller=self) =
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, page_name):
frame = self.frames[page_name]
frame.tkraise()
def get_page(self, page_class):
return self.frames[page_class]
def open_file(self):
name = askopenfilename(
filetypes=((".Out File", "*.out"), ("All Files", "*.*")),
title="Choose a file.")
message = ("File location: " + str(name))
print(message)
В приведенном ниже кадре я пытаюсь разместить радиокнопку и, в зависимости от выбранного пользователем значения, вывести значение.
class ConvBehaviour(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="See behaviour", font=LARGE_FONT)
label.pack(pady=10, padx=10)
button1 = ttk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(MainPage))
button1.pack()
button2 = ttk.Button(self, text="See points statistics",
command=lambda: controller.show_frame(GaussPoints))
button2.pack()
norms = [("norm 1", 1),
("norm 2", 2),
("norm 3", 3)]
#Here is the problem
self.v1 = tk.IntVar()
self.v1.set(1)
for text, num in norms:
radiobutton = tk.Radiobutton(self, text=text, value=num, variable=self.v1, command=self.show_choice)
radiobutton.pack()
def show_choice(self):
print('int ' + str(self.v1.get()))
У меня проблема в том, чтометод show_choice (self) не работает в текущей реализации. Он выводит текущее значение, установленное в self.v1.set (1), вместо 1, 2, 3. В зависимости от выбора пользователя. Где находится проблема?