Error creating python tkinter radio button after saving

Asked 2 years ago, Updated 2 years ago, 93 views

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

2022-09-22 19:14

1 Answers

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()


2022-09-22 19:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.