import tkinter as tk
def func1():
label.config(text = 'Button 1')
def func2():
label.config(text = 'Button 2')
sel = tk.IntVar()
sel.set(1)
label = tk.Label(root, text = 'Select Button')
label.pack()
rb1 = tk.Radiobutton(root, text = 'Button 1', variable = sel, value = 1, command = func1)
rb1.pack()
rb2 = tk.Radiobutton(root, text = 'Button 2', variable = sel, value = 2, command = func2)
rb2.pack()
root.mainloop()
I don't think I wrote anything wrong, but I tried to save it and run it, but it didn't work Why is that?
python tkinter gui
root is not defined.
import tkinter as tk
def func1():
label.config(text="Button 1")
def func2():
label.config(text="Button 2")
root = tk.Tk()
sel = tk.IntVar()
sel.set(1)
label = tk.Label(root, text="Select Button")
label.pack()
rb1 = tk.Radiobutton(root, text="Button 1", variable=sel, value=1, command=func1)
rb1.pack()
rb2 = tk.Radiobutton(root, text="Button 2", variable=sel, value=2, command=func2)
rb2.pack()
root.mainloop()
© 2024 OneMinuteCode. All rights reserved.