Home > OS >  tkinter window opens but no widget shows up
tkinter window opens but no widget shows up

Time:12-19

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?

CodePudding user response:

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 *
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!!

  • Related