Recently I tried creating the function in which all items on a TKinter GUI will resize themselves to fit the screen, but I cannot seem to figure out how to do this. I did try this:
from tkinter import *
root = Tk()
root.geometry("710x300")
def dimloop():
root.update()
screenwidth = root.winfo_screenwidth()
print(root.winfo_screenwidth())
button.config(width=round(screenwidth/7.1))
root.after(10, dimloop)
button = Button(root, text = 'Hello', width=100)
button.place(x=0, y=0)
dimloop()
root.mainloop()
But That Did Not Work. Does Anyone Know Any Way To Do This?
The screenwidth function returns the width of the physical screen. That's not going to change.
If you want the width of the root window you should use root.winfo_width(). There's no point in doing this in a loop, you can get the width of the screen and then set the window width and height using the geometry method to force it to be a particular size.
Related
I want to get the coordinates of the button when I click on the button in tkinter. How can I do this? Please help.
The winfo_* methods will give you information about a window. For example, winfo_rootx and winfo_rooty will return the screen (not window) coordinate of the upper left corner of the widget. winfo_x and winfo_y will return the coordinate of the widget relative to its parent. You can get the width and height with winfo_width and winfo_height.
try this small example.
import tkinter as tk
root = tk.Tk()
btn = tk.Button(root, text=str(1))
btn.pack(padx=10, pady=10)
root.update()
widget_x, widget_y = btn.winfo_rootx(), btn.winfo_rooty()
print(f'{widget_x}, {widget_y}')
root.mainloop()
I am trying to make a window that would take an input through an entry and that would be either a web address or ip address and i would use a loop to update the text of a label to show the current ping every second. But I'm stuck at the very beginning because my entry would not appear on my window. Here is my code:
import tkinter as tk
from tkinter import *
window = tk.Tk()
window.title("Server Status")
window.geometry('400x600')
window.resizable(0,0)
canvas = tk.Canvas(window,height=600,width=1000,bg='#263D42')
canvas.pack()
txtf=tk.Entry(window, width=10)
txtf.pack()
window.mainloop()
Where am I going wrong? I have tried it with several changes but still cant get it to appear there. Any help would be appreciated.
Thanks.
Your entry is below the canvas, but because (1) your window geometry specifies a smaller size than that requested for the canvas, and (2) you set it to be non resizable, you can never access it.
Choose how to resolve this conflict; the example below sets the size of the canvas, and lets the window resize to enclose all its widgets.
import tkinter as tk
window = tk.Tk()
window.title("Server Status")
canvas = tk.Canvas(window, height=600, width=1000, bg='#263D42')
canvas.pack()
txtf = tk.Entry(window, width=10)
txtf.pack()
window.mainloop()
So I have one Tkinter screen that has a canvas. I want to change the size of the canvas by creating a new window that has entry widgets. So I created a new screen and added 2 entry widgets. I want to get the value from those widgets and based on that...it should change the size of the canvas. I tried to do this for an hour, but no luck. Please assist me.
Here is my code
from tkinter import *
# create root window
root = Tk()
# Create Canvas
canvas = Canvas(root, width=50, height=50)
# Create an additional window (the one that is used to enter the new geometry)
dialog = Toplevel(root)
# Add entry widgets for width and height to the new window
width_entry = tk.Entry(dialog)
height_entry = tk.Entry(dialog)
# Add a button to the new window that applies the given width and height
apply_button = Button(dialog, text = 'Apply geometry', command = lambda: canvas.geometry(width_entry.get()+'x'+height_entry.get()))
# Its not possible to get the geometry of a canvas in tkinter...so how do I change the size.
# display the entry boxes and button
width_entry.pack()
height_entry.pack()
apply_button.pack()
# start the tk mainloop
root.mainloop()
Please Assist me
The command you are looking for is canvas.config
Here, I have adjusted the given code:
import tkinter as tk
# create root window
root = tk.Tk()
# Create Canvas
canvas = tk.Canvas(root, width=50, height=50)
canvas.pack()
# Create an additional window (the one that is used to enter the new geometry)
dialog = tk.Toplevel(root)
# Add entry widgets for width and height to the new window
width_entry = tk.Entry(dialog)
height_entry = tk.Entry(dialog)
# Add a button to the new window that applies the given width and height
apply_button = tk.Button(dialog, text = 'Apply geometry', command = lambda: canvas.config(width=width_entry.get(), height=height_entry.get()))
# display the entry boxes and button
width_entry.pack()
height_entry.pack()
apply_button.pack()
# start the tk mainloop
root.mainloop()
I also changed a couple other things:
You imported * from tkinter, but for some items you still led with tk.; I changed them all to match that and switched the import to match as well. (You could still use *, but then just don't have the leading tk.s.)
The canvas was never packed so you could never see what was going on there.
One more suggestion, that line where you make the button is really long. Maybe make a function that does what the lambda does and assign its command to that function instead of a lambda. You can probably see that a line that long is even hard to read here much less if someone (maybe a future version of yourself) was to try to read your code, and edit it or make sense of it. Generally, try to keep all lines down to 80 characters.
Let us know if you have any more questions etc.
I am trying to make a circle in tkinter change colors after the window has been initiated. I've looked at this question, and I know how to change the colors after stating the variable. I'm trying to make a traffic light (much like the person in the question I looked at), but I can't update the color change after the screen comes up. This is what I have so far
root = tk.Tk()
canvas = tk.Canvas(root)
light_1 = canvas.create_oval(*coordinates here*, fill='green')
root.mainloop()
and to change the color use canvas.itemconfig(light_1, fill='blue')
and I can't just do a time.sleep(1) because then the root.mainloop() is only reached after i change the color. There is no visual feedback of it changing
You can't use time.sleep() anywhere in tkinter code because it blocks the tkinter mainloop from running. The solution is to add your code to the tkinter mainloop using the after method:
def change_color():
canvas.itemconfig(light_1, fill='blue')
root = tk.Tk()
canvas = tk.Canvas(root)
light_1 = canvas.create_oval(*coordinates here*, fill='green')
root.after(1000, change_color) # 'after' uses milliseconds, so 1,000 = 1 second
root.mainloop()
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.