Is there a way to disable the window resizing in the Turtle module?
E.G - Disable the maximize and minimize button and disable the ability to drag the window out or in. Thanks!
There's another way of doing it which is a little more 'hacky' but works well for projects that are already written using TurtleScreen and not a RawTurtle. It is actually a one-liner:
screen = turtle.Screen()
# ...
screen.cv._rootwindow.resizable(False, False)
This accesses the root window of the scrollable canvas object that turtle creates and calls the resizable method on it. This is not documented, though - so it might produce unexpected behavior.
As a general remark: Whenever you want to use functionality of tkinter in a turtle program and you cannot find a turtle method for it - just check turtle's sources, figure out how turtle abstracts away the tkinter object (like the canvas in this case) and use the appropriate method on that object directly. Probably doesn't work all the time - but mostly you'll be able to achieve what you want.
Python turtle is built atop tkinter. When you run the turtle module standalone, it creates a tkinter window, layers it with a scrollable canvas and wraps in a screen object that provides lots of niceties for working with the turtle. But you can instead run the turtle module embedded i.e. build whatever kind of tkinter window you want and run turtle inside it.
Here's a very simple example of a window with a turtle drawing that's not resizeable:
from tkinter import *
from turtle import RawTurtle
root = Tk()
root.resizable(False, False)
canvas = Canvas(root)
canvas.pack()
turtle = RawTurtle(canvas)
turtle.circle(10)
root.mainloop()
Related
very simple problem, I just try to make tkinter window that you are not able to move with the mouse. the problem is that I don't find a function that can do it, the only thing that I found is making it not resizable and stuff like that, I also found a function that hides the bar and making it not resizable or moveable, but it doesn't help me cause I still want it to have the default window bar, let's just say I have a normal window here:
import tkinter as TK
window = TK.Tk()
window.geometry('1200x800')
window.mainloop()
so just tell me if you know how to make it impossible to move the window with the mouse, how to do that, the code should work fine with fullscreen and other functionality like that by the way.
I created a .py script that autofill some forms using Selenium. In the start of the code, I used Tkinter extension to use the messagebox and filedialog function.
My code runs fine, but everytime I got an annoying window called "tk" (I guess it is related to Tkinter). This window is blank, but I would like to remove it from my code run.
Is there a way to do this?
Try this:
import tkinter as tk
from tkinter import filedialog
# Create a dummy window
root = tk.Tk()
# Hide the window
root.withdraw()
# Use `filedialog` freely
print(filedialog.askopenfilename())
# If you want to destroy the window at the end.
# You don't have to
root.destroy()
The reason why that window appears is linked to how tkinter handles new windows. It uses tkinter.Toplevel instead of a tkinter.Tk. But a tkinter.Toplevel can't exist without a tkinter.Tk so it creates one. That is the window that you see.
To hide the window you have to first create your own tkinter.Tk and hide it using <tkinter.Tk>.withdraw().
I have tried to find this answer but with little to no luck surprisingly.
Here is what I am running:
Python 3.8.5 64 bit
TKinter 8.6
The two sources for screen size that I have found are:
import tkinter as tk
from win32api import GetSystemMetrics
screen_height = GetSystemMetrics(1)
screen_width = GetSystemMetrics(0)
mainWindow = tk.Tk()
mainWindow.configure(width=screen_width, height=screen_height)
mainWindow.mainloop()
The issue with this method is that it creates a window from corner to corner inclusively that is the exact size of my screen, meaning that with the task bar and borders considered, the app clips off the screen and also 'under' the task bar.
There was also this method I found:
import tkinter as tk
mainWindow = tk.Tk()
mainWindow.state("zoomed")
mainWindow.mainloop()
This does exactly what I would want, imitate the way the app would look if I were to click the maximize button, lined up with both edges of the screen and 'resting' on top of the task bar.
However if I want to create a canvas with the same size as the window, but using winfo_height() and winfo_width() on my Tk object the returned values are 1 and 1 respectively. I'm not sure if this is because those values are set before setting the state to 'zoomed' but the height and width arguments in tk.Canvas have units of either pixels (default), inches, millimeters, centimeters, or print points. Thus I cannot use those exact values to create the canvas.
import tkinter as tk
mainWindow = tk.Tk()
mainWindow.state("zoomed")
mainCanvas = tk.Canvas(height=mainWindow.winfo_height(), width=mainWindow.winfo_width(), bg="blue")
mainCanvas.pack()
mainWindow.mainloop()
Lastly there is the attributes section that could be utilized:
#blah code the same as before
mainWindow.attributes("-fullscreen", True)
#blah code till the end as before
This unfortunately creates a fullscreen app as if you hit F11 on most browsers or fullscreened a youtube video, etc. This is not what I want obviously.
Do I need to create a listener that waits until the window state has been changed to zoom before grabbing the height and width of the application? Or is there some way to simply start the application in a 'maximized' state as if the maximize button were pressed by default.
I couldn't believe the difficulty in finding anything on the tk class' .state method, nor any information really about simply maximizing a TK app window. Everything I found landed in one of the three examples I shared and I would greatly appreciate any help anyone could offer!
Thank you
Instead of trying to create the canvas at the correct size, create it at the minimum size you think it should be, and then let one of tkinter's geometry managers expand it to fill the window.
For example, if you do mainCanvas.pack(fill="both", expand=True) it will cause the canvas to fill the window without you having to compute the size of the canvas, and without having to call update during startup.
I have made a GUI using Tkinter for my Python script for a Voice Assistant. It is working pretty well. But I want to add an animation window to display an animation that I have created using After Effects as an intro to the app. I want it to open without the default close(x), maximize and minimize buttons. The window should stay till the animation is completed, and then it would disappear. The main window would then open normally to launch the GUI. To disable the close, maximize and minimize buttons I have used the root.overrideredirect(True) method. But I am not being able to simultaneously open the two windows one after the other as mentioned. I would be highly obliged if somebody would help me out with this! I have tested it with a code on a simple GUI. I am providing the code below for help with the problem!
from tkinter import *
import time
root = Tk()
root.geometry('500x300')
root.overrideredirect(True) # To disable the default Window decoration
time.sleep(5) # Assuming that the animation runs for 5 seconds
root.destroy() # This window destroys after being on screen for 5 seconds
root.mainloop()
root2 = Tk() # After the previous window is destroyed, this window opens up. Assume that this is the main window
root2.geometry('500x300')
root.mainloop()
Please help me out!
The thing you call "animation window" is actually called "splash". There is a way to do what you want. You need to create a root window for the app (a Tk instance), and then you should hide (root.withdraw()) it. Now create a Toplevel for the splash, wait for 5 seconds, destroy it, and show (root.deiconify()) the Tk window again.
Note: time.sleep(5) should never be used with Tkinter, use root.after(ms, func) instead.
from tkinter import *
def show_splash():
splash = Toplevel()
splash.geometry('500x300')
splash.overrideredirect(True) # To disable the default Window decoration
splash.after(5000, splash.destroy) # This window destroys after being on screen for 5 seconds
splash.wait_window()
root = Tk()
root.withdraw()
show_splash()
root.deiconify()
root.mainloop()
PS: Procedural programming is not a good idea for pretty complex Tkinter-based apps. You should consider using OOP instead.
PPS: Take a look at this and this answers.
Here is the code:
from tkinter import *
root = Tk()
root.geometry("350x350")
root.minsize(250, 250)
root.maxsize(500, 500)
root.mainloop()
When I run this and click the Maximize button (the three buttons on the top right of any program, _◻✖) The window automatically snaps to the top left corner. Is there any way to manipulate this behavior and make it, for instance snap to the center of the screen, or the top right, or the top center?
Is there any way to manipulate this behavior and make it, for instance snap to the center of the screen, or the top right, or the top center?
No, there is not. That button is not controlled by tkinter. That button requests that the window manager set the window to a mazimized state. AFAIK, all window managers define that as the window filling the screen.
You could bind the <Configure> event and check the wm_state() property. If this method returns 'zoomed' then the window is maximized and you could unmaximise it (wm_state('normal')) and reset its position to the screen center using the geometry method.
If you are doing this for a splash-screen or something then this is probably not the right way to go and you should look up wm_overrideredirect or consider making this a toolwindow which does not show a maximize button (root.wm_attributes('-toolwindow', 1)).