How can I show a window created with Tkinter.Tk() outside visible screen? I need to make it much bigger than the desktop size, and show part of it defined by coordinates.
Use Tk.geometry with desired width, height and negative position.
from Tkinter import * # from tkinter import * (In Python 3.x)
root = Tk()
root.geometry('3000x3000+-100+-100')
root.mainloop()
I tested this on Ubuntu 12.04 (gnome) and Window 7.
In Ubuntu, it work well.
In Windows, negative position works, but width, height higher than resolution ignored.
Another possible way is to insert a frame and resize that, eg:
import tkinter as tk
root = tk.Tk()
frame = Frame(root, width = 1000, height = 1000)
frame.pack()
root.mainloop
The size of your window will then be determined by the frame, although the answer already given works just fine too
Related
Here is my code so far:
import os
from tkinter import *
import tkinter as tk
system_properties=("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Administrative Tools\System Information.lnk")
device_manager=("devmgmt.msc")
root = Tk()
#root.title()
window = tk.Tk()
root.geometry("200x200")
def open_app():
os.startfile(system_properties)
os.system(device_manager)
Button(root, text ='Open',
command = open_app).pack(side = TOP,
pady = 10)
root.mainloop()
To clarify I am not looking to change the tkinter window. What I want to do is open multiple applications at once on Windows 11 with no overlap like in a split screen format, but I am not sure how to incorporate code to make the windows display with my desired size and position. Currently, they are opening on top of each other with different window sizes.
The following will set a tkinter window width and height.
root.geometry("500x500")
Is it possible to only set width or height?
Where can I find a full list of geometry method variations, for setting only select parameters?
I would like to be able to set select parameters, of the window size and/or position, and let the rest be under tkinter dynamic control.
When looking at the tcl/tk documentation, we can see that we do not have to provide a full "widthxheight±x±y" string to the .geometry() method:
with only "widthxheight", the size of the window will be changed but not its position on the screen.
with only "±x±y", the position will be changed but not the size.
However, it is not possible to set separately the width and height of the window with this method. Nevertheless, you can retrieve the dimension you don't want to change and use it in .geometry() with something like
def set_height(window, height):
window.geometry("{}x{}".format(window.winfo_width(), height))
def set_width(window, height):
window.geometry("{}x{}".format(width, window.winfo_height()))
Note: Using root.config(width/height=...) only works for me if the window has never been resized with the mouse or using .geometry()
Use root.config(width=100) or root.config(height=100)
If you define a frame within your window you can set width and height of this frame separately like this:
myframe = tk.Frame(width=200)
This may create the appearance you wish to achieve.
An example program demonstrates how to use this trick:
import tkinter as tk
window = tk.Tk()
window.title('Frame Demo')
frame = tk.Frame(width=300)
frame_2 = tk.Frame()
label = tk.Label(master=frame_2, text="Frame_2")
label.pack()
frame.pack()
frame_2.pack()
window.mainloop()
The frame by the name frame is used only to widen the window and is invisible. No GUI elements should be placed in it. Its only function is to give the main window the needed size of 300, in this example. frame_2 is the canvas, so to say, for the GUI elements.
I am creating a project using tkinter and when I create a window, I couldn't seem to get the window title to center itself (Like most programs nowadays). Here's the example code:
from tkinter import *
root = Tk()
root.title("Window Title".center(110))# Doesn't seem to work
root.mainloop()
Is there a way to center the window title up ? Thanks in advance
There is nothing you can do. Tkinter has no control over how the window manager or OS displays the titles of windows other than to specify the text.
I came up with a trick that does the job and it consists in simply adding as much blank space before the title:
import tkinter as tk
root = tk.Tk()
root.title(" Window Title")# Add the blank space
frame = tk.Frame(root, width=800, height=200, bg='yellow')
frame.grid(row=0,column=0)
root.mainloop()
Output:
Alternatively, you can use a string consisting of an empty space and concatenate it to the title after multiplication. I mean:
import tkinter as tk
root = tk.Tk()
blank_space =" " # One empty space
root.title(80*blank_space+"Window Title")# Easier to add the blank space
frame = tk.Frame(root, width=800, height=200, bg='yellow')
frame.grid(row=0,column=0)
root.mainloop()
More adding onto what Billal suggested is this example that adjust depending on the window size. I still wouldn't recommend it since it's just a hack for visual aesthetics but if you really want to have it.
import tkinter as tk
def center(e):
w = int(root.winfo_width() / 3.5) # get root width and scale it ( in pixels )
s = 'Hello Word'.rjust(w//2)
root.title(s)
root = tk.Tk()
root.bind("<Configure>", center) # called when window resized
root.mainloop()
width=root.winfo_screenwidth()
spacer=(" "*(int(width)//6))
root.title(spacer+"Your title")
This is not that much perfect but this will work.
I have designed an application using the Tkinter module from Python, on my 17"
screen.
Is there a way to make this app fit on lower resolution screens? I have tried to run it on a 14" screen, and the app doesn`t fit well.
Thank you.
You can get the screen resolution then input them in your root.geometry, this way:
from Tkinker import *
root = Tk()
width, height = root.winfo_screenwidth(), root.winfo_screenheight()
root.geometry('%dx%d+0+0' % (width,height))
root.mainloop()
You need to use this one line:
root.state('zoomed') #works on all operating systems
root = tkinter.Tk()
root.state('zoomed')
This will fit the window perfectly to the display. (Note: This is not full screen function. Full screen means the window will also cover the taskbar, but in this code window fits all over the screen except the taskbar)
When I try to detect that a window is resized with tkinter, it works most of the time. However, there is one exception; that is, when resizing it for the first time vertically and making the window larger, nothing is picked up. I see the initial size when the window appears, but nothing after that if I try to immediately make the window larger vertically. However, if I try to make it vertically "taller," after resizing it another way (ie making it smaller or resizing horizontally), then it works fine.
Here's the gist of the code I'm using:
from Tkinter import *
main = Tk()
def resize(event):
print("The new dimensions are:",event.width,"x",event.height)
window.focus_set()
canvas = Canvas(main, width=850, height=400)
window = Frame(main, width=800, height=10)
window.focus_set()
canvas.bind("<Configure>", resize)
canvas.pack()
canvas.update()
window.pack()
main.mainloop()
Am I doing something wrong?
If it matters, I'm using Python 2.7.3 on 64 bit Ubuntu 12.10
EDIT: It seems as though the dimensions are either incorrect (horizontal enlargement) or not showing up at all (vertical enlargement) when the window size is above 852x402. Is this a problem with my window manager (ie, Unity)?
Change canvas = Canvas(main, width=850, height=400) by canvas = Canvas(main, width=850, height=400, bg="red") and see what happens.
Since the canvas is inside the root window, when you make the window vertically larger, you are not changing the canvas size or position. It maintains its maximum height of 400, and the handler is not called. However, in the case of the horizontal enlargement, its position is exactly the middle because of the call to pack(): That's why it moves, and the bound function is called.
The rest of the results when you make the window smaller both horizontally and vertically are as expected.
It's because the canvas is not being resized. You're setting the binding on the canvas, but you haven't configured the canvas to grow and shrink along with the containing window.
Try setting the background of the canvas and frame widgets to something distinctive and you'll see what I mean.
To fix the problem try setting the expand and fill attributes when packing your widgets.