Python tkinter components not showing until clicked on in a Jupyter notebook - python

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

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

Python's Tkinter's iconify not working on Fedora

This code is used to hide or dock the current window, and then create a new window using CTkTopLevel, and then to automatically undock or bring back the previous window to the screen. This seems to work on OSX perfectly, but when I try it on my Fedora machine, it fails to undock? Any reason why?
This is the code, called in both machines.
def CreateNewAccount(self):
#hide the previous window // seems to only work on OSX well
self.iconify()
#create new window
window = customtkinter.CTkToplevel(self)
window.title("Project")
window.geometry("1200x800")
window.resizable(False, False)
window.protocol("WM_DELETE_WINDOW", self.deiconify())
It should automatically undock and dock when deleting the window, tried other things like withdraw and it didn't work. Sometimes got a bug where after deleting the second tab, it would stay there as an unreactive tab and would only disappear after I closed the whole program.

Why is everything freezing when I'm using Tkinter's file dialogue windows?

Sorry this is so long but I'm trying to be detailed.
I found a similar question, but it was related to an import that I'm not using (so was the solution, so I'm not sure how relevant it is). The TL;DR is that I'm learning Tkinter and learning how to open files with it, but no matter what I do it's freezing.
The longer version is this - I'm trying to make an image opening program. Nothing fancy, just press a button to pull up the file dialogue screen and then open an image. The only other button in there is the exit button that works fine.
I made the whole thing and it mostly worked except for the aforementioned freezing (meaning it would actually display the image). I'm using PyCharm and it freezes so bad that I have to force quit Python and PyCharm.
Things I've tried:
Stripping away parts of the code to see what appears to be causing the freezing. That code is below.
Tested another little practice program with similar functionality (just an image viewer that lets the user cycle through images in a predetermined folder with no file dialog). It doesn't seem to be a problem with how I'm making buttons or anything since everything works in that program.
The issue in the code is at the line starting with "filedialog" and ending with the triple parentheses. If I "open" an image, nothing should happen, but even if I make it successfully open something, it will freeze anyway. I won't be able to close it with the "Exit" button and I won't be able to force quit it through PyCharm. I have to actually stop it through the Task Manager.
Here's the stripped down code that only has the dialog opening and the exit button:
from tkinter import *
from PIL import ImageTk, Image
from tkinter import filedialog
root = Tk()
root.title('File dialogue')
filedialog.askopenfilename(initialdir='/image/', title='Pick an image', filetypes=(('png files', '*.png'), ('all files', '*.*')))
Button(root, text='Exit', command=root.quit).pack()
root.mainloop()

Why does my "Hello World" Tkinter program only open a blank, black screen?

I am teaching myself python, but am running into problems with Tkinter. I created a simple program based on some videos I've been using to learn as I go. From all my research thus far, I cannot find a problem in the code, it is only four lines show below. The code creates a window called "tk" that is completely black with no text inside that is the correct size for what the text should take up if it were to be there.
I have tried to select the text inside the window to see if there is anything there, but there isn't. I've tried to change the background color to white using the bg command, but nothing changes with the window. I have opened "empty" windows using only the first and last line, which again creates a window of the correct size from everything I've seen online, but again is completely blank and black instead of white or gray as it should be.
I am using PyCharm (PyCharm 2021.3.1 (Community Edition)) as my compiler on a MacBook Pro. (iOS is on 12.0.1). Tkinter is on (Tcl 8.5 & Tk 8.5 (8.5.9)).
When I click run, everything seems to run smoothly with no errors. Honestly, I'm at a complete loss as to what the problem is. Any ideas on how to fix this?
from tkinter import *
root = Tk()
myLabel = Label(root, text="Hello World! Why can't you see me?")
myLabel.grid(row=0, column=0)
# root.configure(bg='white')
root.mainloop()
I had a similar issue to this due to running python 2.7.
When we updated to python 3.10 the issue disappeared.
Try checking your version of python.

How to remove window frame from program using Tkinter and Matplotlib

I am writing a graphical program in Python for my Raspberry Pi project. I have started writing it using Tkinter and wish to use the Matplotlib tools.
Due to limited screen space and the purpose of the project, I want it to be fullscreen without a window frame and menubar showing. Normally I use the following command:
app.overrideredirect(1)
This works great until I import Matplotlib. Once I do that, the window frame appears again even with the above line of code.
How can I get Matplotlib to not show the window frame or menubars and be completely fullscreen?
Thanks!
I found the problem. The example code I followed involved using the command canvas.show() along with canvas.get_tk_widget().grid(...). The canvas.show() was not needed and caused it to override the app.overrideredirect(1) command.

Locking terminal size in python?

I'm wondering if there is any python code I can use to lock the size of my CMD window? I'm making a small text adventure game and want t at a specific size.
If it helps, I'm using windows 7, but wouldn't mind mac and linux compatible instructions. Thanks.
I don't think this is possible. If you want to control a gui directly, you should use a gui package like tkinter
for example
from Tkinter import *
window = Tk()
window.minsize(minw, minh)
window.maxsize(maxw, maxh)
add a text block to this and treat it just like a cmd window. You can read more about it on the Tkinter docs
you can use pyWinGUI for emulate text terminal with comctl.Edit control

Categories