AR_Hasani
AR_Hasani

Reputation: 138

tkinter window opens but no widget shows up

I have just started working with tkinter and written the below simple code. However, after running no widget appears in the opened window.

import tkinter;
root = tkinter.Tk ();
root.geometry ("400x450");

main_window = tkinter.Frame (root);
main_window.pack ();

mode = tkinter.Frame (main_window);
if_tx = True;
tx_switch = tkinter.Radiobutton (mode , text = "Tx" , variable = if_tx , value = True);
tx_switch.pack (padx = 5 , pady = 5);
rx_switch = tkinter.Radiobutton (mode , text = "Rx" , variable = if_tx , value = False);
rx_switch.pack (padx = 5 , pady = 5);

root.mainloop ();

The only thing that appears is this:

enter image description here

What could be the problem?

Upvotes: 0

Views: 69

Answers (1)

Swagrim
Swagrim

Reputation: 422

I guess you are new to python. In python, we don't use semicolons to mark the end of sentences. Also while importing tkinter, we have to import everything from it instead of import itself. And finally, you aren't packing the mode variable.

The correct code:

from tkinter import Tk, Frame, Radiobutton
root = Tk()
root.geometry("400x450")

main_window = Frame(root)
main_window.pack()

mode = Frame(main_window)
mode.pack()
if_tx = True
tx_switch = Radiobutton(mode, text="Tx", variable=if_tx, value=True)
tx_switch.pack(padx=5, pady=5)
rx_switch = Radiobutton(mode, text="Rx", variable=if_tx, value=False)
rx_switch.pack (padx=5, pady=5)

root.mainloop()

Hope it helped you!!

Upvotes: 1

Related Questions