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.
Related
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.
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.
I'm having a problem with changing the background of this part of the window:
How do I change it?
As I see you are using windows.
This color is set by the theme you are currently using. It is the same for every window.
So I cross out the possibility of only using the Tkinter module for this.
Tkinter is responsible for what is in the window but the window manager decides about the border. For example in Ubuntu the window would look totally different.
I guess, you would need some windows specific calls for that.
You can remove the border with root.overrideredirect(1) if I remember correctly.
PS: put "windows" into the tags of this question.
How necessary is viewing the canvas window that a Turtle object draws on? Can it be hidden or disable and just the final graphics captured?
I have a python script that uses the turtle library and am wondering is it crucial for the canvas window, where one can view the actual drawing being created, to be open during the script's runtime? Is it possible to not even load the visual portion of the canvas and simply capture the data at the end? (which btw I create a .ps file with and use PIL to convert it to a .jpg).
Would this perhaps make the overall script run faster as I am planning on bumping up my iteration count significantly and it is already fairly slow as is (which could of course be the nature of the library and I have to just deal with it)?
Having searched Google, the only issues I could find with terms like, "hide turtle canvas" or "disable canvas window" just netted me issues unrelated to my question. I have also searched through the turtle library documentation and no function or methods seemed to do the job, but of course I could have missed it.
I'm not familiar with the root package tkinter so if there is something on that end that I could mess with I can look into that as a solution. But I wouldn't know where to begin as I don't fully comprehend the relationship between tkinter and turtle.
Using embedded turtle, we can create and withdraw the Tk root window immediately and then simply work with a Canvas:
from tkinter import *
from turtle import RawTurtle
(root := Tk()).withdraw()
canvas = Canvas(root)
turtle = RawTurtle(canvas)
turtle.speed('fastest')
turtle.begin_poly()
for _ in range(5):
turtle.forward(100)
turtle.right(360/5)
turtle.end_poly()
print(turtle.get_poly())
OUTPUT
> python3 test.py
((0.00,0.00), (100.00,0.00), (130.90,-95.11), (50.00,-153.88), (-30.90,-95.11), (0.00,0.00))
>
I'm intentionally avoiding the TurtleScreen and ScrolledCanvas wrapper classes as we don't need any interactive methods.
The same thing could probably be done from standalone turtle by digging into the underpinnings via getcanvas() and finding the root window to withdraw.
Not perfect, but on my system I don't see any window. If you see a flash of one, look up the tkinter overrideredirect(1) method, calling it just before the withdraw() to avoid the flash.
I've created an application which after choosing some files and clicking a specific button, it generates 3 to 5 plots using matplotlib (and some uses plotly). For this GUI I used Tkinter.
When I run the .py code it works perfectly, after pressing the button a plot is shown, then after closing its window a new one is displayed and so on until finish.
I tried creating the .exe file for this application using Py2exe and PyInstaller. Both of them have the same behaviour: after pressing the button the first plot is shown, but when I close its window no one is displayed. Only after closing the Tkinter windows created (one main Frame that creates another Frame which has the buttons I click for displaying the plot) the other plots appear (the second one, then after closing its window the third one, and so on).
Any clue about this? I've tried also
import matplotlib
matplotlib.use('TkAgg')
without success. TkAgg is the one that worked for generating the .exe (had to change it in the matplotlibrc).
The code runs the following way: main_app with Tkinter functionalities, second_app with some useful functions and only one Tkinter message (if conditions satisfied) and a bunch of minor .py files that each one is a function with its own calculations and plots.
If a button from the main_app is pressed it calls one specific of these minor .py files (functions) for calculating and generating the plots.
I ran into the same problem. I was using Python 3.4 and PyInstaller 3.3, and I would try to make several plots at once but only one would show until I closed the whole executable (made with PyInstaller's --onefile option). I solved the problem by using
plt.show(block=False)
instead of
plt.show()
when plotting. This did the trick for me, hope it helps you!