Home > Mobile >  Python tkinter components not showing until clicked on in a Jupyter notebook
Python tkinter components not showing until clicked on in a Jupyter notebook

Time:03-24

I am trying to practice a basic GUI using Python tkinter and Jupyter notebook. I am working on Mac Monteray. The problem I am having is that the Scale, Message and Spinbox elements don't show up until the space where they are located is clicked on. This happens most of the time but occasionally they do show as expected.

I have created a new notebook with just the Spinbox component, to make sure it wasn't other code interfering but I get the same result. Has anyone else come across this problem?

This is my code

from tkinter import *

# create root window
root = Tk()
 
# root window title and dimension
root.title("GUI Components")

# Set geometry (widthxheight)
root.geometry('1350x1250')
 
# all widgets will be here
text = Label(root, text='GUI Components')
text.pack()

#SpinBox – select from a range of values using arrow (up/down) selectors.
spnlabel = Label(root, text = 'Spin box')

sp = Spinbox(root, from_ = 0, to = 20)

spnlabel.pack()
sp.pack()

root.mainloop()

CodePudding user response:

Since tkinter predates jupyter notebooks by decades, it was never conceived to run in such an environment.

Save your script as a file, and run it directly with Python. It works fine in that case.

Edit: You should be aware that there are several things in the Python ecosystem that don't work well with jupyter notebooks. GUI toolkits like tkinter are one thing. And multiprocessing or concurrent.futures is another.

In general, if your Python code doesn't run or does weird things in a fancy IDE, try saving it as a script and run it from the command line first.

  • Related