I want that after starting the program, it checks its actual size and when "window.winfo_width > 520" it changes its size to 520. Is it possible?
from tkinter import *
from time import *
def upd():
tstr = strftime("%I:%M:%S %p")
ltime.config(text=tstr)
lday.config(text=strftime("%A"))
dtstr = strftime("%B %d, %Y")
ldate.config(text=dtstr)
ltime.after(1000, upd)
window = Tk()
window.config(bg='black')
window.title("Clock")
window.geometry('520x300')
window.resizable(0,1)
ltime = Label(window, font="Adobe 60", text='', fg='green', bg='black')
ltime.pack()
lday = Label(window, font="Adobe 60", text='', fg='green', bg='black')
lday.pack()
ldate = Label(window, font="Adobe 60", text='', fg='green', bg='black')
ldate.pack()
upd()
window.mainloop()
Firstly, window.winfo_geometry() will give you the geometry of the window.
For getting width only, use window.winfo_width().
For getting height only, use window.winfo_height()
e.g.
if window.winfo_width() > 520 :
window.geometry('520x300')
Now, since you are using window.resizable(0,1), you are disabling the window's width to be resized, so windows's width will be fixed to 520.
So, how will it exceed 520, if it's not resizable?
If you enable it, and then want window's width to not exceed 520:
Tkinter offers .maxsize() and .minsize() to fix the size of the root window.
e.g. if you want that the window's width to not exceed 520, then just set the width in maxsize as 520.
window.geometry('520x300')
window.maxsize(520, 300)
The window's width will not be resized more than 520 now.
In case, you want to resize according to the user's screen's size:
To get the screen size, you can use winfo_screenwidth() which returns the screen width and winfo_screenheight() for the height of the screen in pixels.
window.geometry() can be recalled if needed.
EDIT: An example of recalling window.geometry() after the start of program(, even after resizing is disabled horizontally):
def resizeScreen():
window.geometry('520x300')
b = Button(window, text="RESIZE", command=resizeScreen)
b.pack()
Whenever this button is clicked, the screen will be resized, from what it is currently.
You can call this same function without using a button, by just calling the function. It depends on your code, how you use it.
Related
When the following code is executed, it gives an error that float numbers are not acceptable. So how do you adjust the height and width with float numbers?
win = Tk()
btn = Button(win, width = 3.5 , height = 4.5)
btn.grid()
win.mainloop()
I am not sure if you want to specify the size of the button in pixels, or specifically in a rational amount of characters, but in the first case skip to the second part of the answer.
In both cases you will have to set the size in pixels, if you want to specify it in a rational amount of characters, you will first have to get the size of these characters:
import tkinter as tk
import tkinter.font as font
win = tk.Tk()
defaultfont = font.nametofont("TkDefaultFont")
width = defaultfont.measure("k") # has character length, not sure if this works on any platform
height = defaultfont.metrics()['linespace']
w = int(3.5*width)
h = int(4.5*height)
Then you have to set the button size in pixels, you have 2 possibilities:
Set a button image, and specify the size in pixels (like Matiiss proposed)
Put a frame with a specific size and make the button span the frame
Like this:
pixelVirtual = tk.PhotoImage(width=1, height=1)
btn = tk.Button(win, text='kkkk', image=pixelVirtual, width=w , height=h, compound="c")
btn.grid(row=0, column=1)
fr = tk.Frame(win, width=w, height=h, borderwidth=0, highlightthickness=0 )
fr.propagate(False)
fr.grid(row=1, column=1, sticky="nsew")
btn2 = tk.Button(fr, text='kkkk')
btn2.pack(expand=True, fill="both")
The result will not be entirely the same as there is some padding when using character sizes with the button (you can see this by making a button of integer sizes with this method compared to making the button with integer sizes in the default character based method.)
I was working on a project and I want to make fixed position with window size I mean if the window resized the position of that widget will be increase \ decrease .
Help
I made this :
but If I increase size of the window look :
the button will be in the same position 😢
Code :
from tkinter import *
from tkinter import ttk
import os
edt_win = Tk()
edt_win.geometry("1280x720")
edt_win.title("Tkinter Editor")
edt_win.minsize(width=900, height=700)
mainfont = ("comic sans ms",10,"bold")
add_obj_menu_frm = Frame(width=200,height=200,relief=SUNKEN,borderwidth=4)
add_obj_menu_frm.pack(side=LEFT,fill=BOTH,ipady=200,pady=50)
def add_obj_layout():
add_btn = ttk.Button(master=edt_win,text="Add")
add_btn.pack(side=BOTTOM,fill=BOTH)
add_btn.place(x=8,y=680)
add_obj_layout()
edt_win.mainloop()
Any Help will be in the heart ♥️
You can use place() for both the frame and the button:
add_obj_menu_frm = Frame(width=200, height=200, relief=SUNKEN, borderwidth=4)
#add_obj_menu_frm.pack(side=LEFT, fill=BOTH, ipady=200, pady=50)
add_obj_menu_frm.place(x=0, y=50, width=200, relheight=1, height=-100) # 50px top and bottom margins
def add_obj_layout():
add_btn = ttk.Button(master=edt_win, text="Add")
add_btn.pack(side=BOTTOM, fill=BOTH)
#add_btn.place(x=8, y=680)
add_btn.place(x=8, rely=1, y=-40) # 40px from the bottom
You are calling the .place() function for your button which fixes the position so it no longer rescales with the application. Try removing that part - the pack() method anyway takes care of drawing your button for you.
I made an application in Tkinter and I want the user to be able to change the canvas size. When I click the button on the root it should open up a dialog that changes the size of the canvas. I also want to add a maximum value, so the user cant pick a width of more than 1300 and a height of 660.
Here is my code:
from tkinter import *
root = Tk()
root.geometry("10000x10000")
canvas = Canvas().pack()
button = Button(root, text="Change Canvas Size").pack()
def changeCanvasSize():
root2 = Toplevel(root)
root2.title("Change the canvas Size")
widthText = Label(root2, text="Canvas Width").pack()
changeCanvasWidth = Entry(root2).pack()
changeCanvasWidth.insert(0, "1100")
heightText = Label(root2, text="Change Canvas Height").pack()
changeCanvasHeight = Entry(root2).pack()
changeCanvasHeight.insert(0, "566")
applySizes = Button(root2, text="Apply", command = lambda: canvas.configure(width=changeCanvasWidth.get(), height=changeCanvasHeight.get()))
root.mainloop()
So how would I set a maximum value, width should be 1300 or less and height should be 600 or less. How would I do that?
why dont you use a if statement? and create a pop-up error message `
if changeWidth.get() > 1300:
newLabel = tk.Label(text="Enter Number Less 1300")
I wrote a short piece of code in tkinter in python to see if I could make a frame appear in my window. Here is the code below:
from tkinter import *
root = Tk()
root.title("Window")
root.state("zoomed")
root.config(bg="white")
winHeight = int(root.winfo_height())
winWidth = int(root.winfo_width())
controlFrame = Frame(root, bg="red")
controlFrame.pack()
root.mainloop()
I created one full-sized window with a background colour of white. The frame inside it is supposed to be red. However, when I run this code, I do not see any red. I am sure I packed it and everything.
I'd love to help you out on this one...
There's just a slight detail that you might not notice right now but the frame, in fact, is present in the window, but it's too small to see. By this I mean you haven't specified the height and width of the frame that you have placed in the window. Here's the fixed version:
from tkinter import *
root = Tk()
root.title("Window")
root.state("zoomed")
root.config(bg="white")
winHeight = int(root.winfo_height())
winWidth = int(root.winfo_width())
controlFrame = Frame(root, bg="red", height = 700, width = 700)
controlFrame.pack()
root.mainloop()
What this will do is just set the height and width of the frame to 700px, so you will get a square frame of red colour.
I hope this answer was satisfactory.
The answer is pretty simple, you don't have any other widget in your frame, it's empty for now, so its size is 0 pixel (or 1, I don't remember). That's why you don't see it in your window.
I'm a newbie to coding, so I assume this is simply a logic error on my end, so forgive me for my inabilities. I am intending for an initial canvas to appear to act as a sort of splash screen for my program, but rather than appearing, the canvas does not show at all, and simply stays invisible until the second canvas is called (after 4 seconds).
root = tk.Tk()
loadScreen = tk.Canvas(root, height = 700, width = 700, bg = "Black")
loadImage = PhotoImage(file="LoadScreen.gif")
load = loadScreen.create_image(2, 2, anchor = NW, image=loadImage)
loadScreen.pack()
root.after(4000, loadScreen.pack_forget())
Usually you would call mainloop() to make sure your windows are updating.
Internally, mainloop() calls
tk.update_idletasks()
tk.update()
I think you can use these to show the window.