From this I found how to resize a button, so I tried to execute this code.
from tkinter import *
selection_window = Tk()
selection_window.wm_title("")
selection_window.geometry('{}x{}'.format(200, 150))
frame_1 = Frame(selection_window, width=200, height=30)
Button(frame_1, text="Single",height = 100).pack(side=LEFT,anchor=S)
Button(frame_1,text="Range",command=Toplevel,height = 20).pack(side=RIGHT,anchor=S)
frame_1.pack()
selection_window.mainloop()
But the size of button is not changed, rather, the buttons went to the center of the window. Can someone please tell me why is the problem?
Button Height:
If you notice, the height of frame_1 is 30 and the height of the buttons are 100 and 20. One button height is significantly taller than frame_1. So if you maximise your tk window, you will see the height difference of the buttons. Alternatively, try setting one button height to 10 and the other to 2, and rerun your script, to see the height difference. Conclusion, the button heights can be changed.
Button Lateral Placement:
The lateral placement of the buttons can be controlled by using the padx=[x_left, x_right] option of the pack system. x_left and x_right denotes the horizontal external padding to be left on each side of the button in relations to it's parent. Your can read Tk documentation for a clearer explanation on the Packer's algorithm.
from tkinter import *
selection_window = Tk()
selection_window.wm_title("")
selection_window.geometry('{}x{}'.format(200, 150))
frame_1 = Frame(selection_window, width=200, height=30)
frame_1.pack()
Button(frame_1, text="Single",height = 10).pack(side=LEFT, anchor=S, padx=[0,40])
Button(frame_1,text="Range",command=Toplevel,height = 2).pack(side=RIGHT, anchor=S, padx=[20,0])
selection_window.mainloop()
Height: Placement:
Part 2:
Per comments below, please run below script to see if changing a ttk.Button height is even possible for OSX using 'non-default' style themes and post your finding in the comment section. It worked on my Ubuntu.
from tkinter import *
import tkinter.ttk as ttk
s=ttk.Style()
print('Style themes on my system are ', s.theme_names())
s.theme_use('clam')
s.configure('bb.TButton', background='white', padding=50)
b1=ttk.Button(text='Default')
b1.pack(side=LEFT, anchor=S, padx=[0,40])
b2=ttk.Button(text='Custom', style='bb.TButton')
b2.pack(side=RIGHT, anchor=S, padx=[20,0])
padding=1
padding=40
Related
am trying to make a notepad app and I wanted to add a small bar at the end with buttons to do custom stuff but when I try to add a button it doesn't appear on the program what did I do wrong?
here is the script
from gc import callbacks
import re
from tkinter import *
from tkinter import ttk
import time
from turtle import right
root = Tk()
root.title('Notepad')
root.iconbitmap('C:/notes.ico')
root.geometry("500x500")
root.tk.call("source", "C:/Users/Hero/Documents/Visual Studio code/My project/azure.tcl")
root.tk.call("set_theme", "dark")
def change_theme():
if root.tk.call("ttk::style", "theme", "use") == "azure-dark":
root.tk.call("set_theme", "light")
else:
root.tk.call("set_theme", "dark")
style=ttk.Style()
style.theme_use('azure-dark')
style.configure("Vertical.TScrollbar", background="grey", bordercolor="black", arrowcolor="white")
scroll = ttk.Scrollbar(root, orient='vertical')
scroll.pack(side=RIGHT, fill='y')
text=Text(root, font=("Georgia, 24"), yscrollcommand=scroll.set, bg='#292929')
scroll.config(command=text.yview)
text.pack()
#button am talking about
fonts = ttk.Button(root, text='Size and Font', style='Accent.TButton')
fonts.pack()
root.mainloop()
Update: It worked when i had the buttons at the top but at the bottom it doesn't show up
The text box is too tall for a window with size 500x500, so the button below the text box is out of the viewable area of the window.
You can set the width and height options of the text box to a smaller values and use text.pack(fill='both', expand=1)` to expand the text box to fill the window:
# set width and height to smaller values
text = Text(root, font=("Georgia, 24"), yscrollcommand=scroll.set, bg='#292929', width=1, height=1)
# then expand it to fill the window
text.pack(fill='both', expand=1)
i fixed by adding a frame then making it at the bottom then putting the button at this frame thanks everyone that tried to help
I would like to place the button boutonexcel at the bottom of the window, in the middle (below both frames). I tried many combination of side=... and anchor=... without success.
How could I place it as I wish?
import os
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from tkinter import messagebox
fenetre_choice_of_mat=Tk()
fenetre_choice_of_mat.geometry("350x500")
class Checkbuttongroup:
def __init__(self, fenetre1, text1):
self.Checkbutton1 = IntVar()
self.texte=text1
self.fenetre12=fenetre1
Button1 = Checkbutton(self.fenetre12, text = self.texte,
variable = self.Checkbutton1,
onvalue = 1,
offvalue = 0,
height = 2,
width = 20,
anchor = "w")
Button1.pack()
cadre_choice_material_1=Frame(fenetre_choice_of_mat,highlightbackground="blue", highlightthickness=2)
cadre_choice_material_1.pack(side='left', anchor=N)
cadre_choice_material_2=Frame(fenetre_choice_of_mat,highlightbackground="blue", highlightthickness=2)
cadre_choice_material_2.pack(side='left',anchor=N)
grp_material_1=Checkbuttongroup(cadre_choice_material_1,"CW")
grp_material_2=Checkbuttongroup(cadre_choice_material_1,"CS")
grp_material_4=Checkbuttongroup(cadre_choice_material_1,"AE")
grp_material_6=Checkbuttongroup(cadre_choice_material_1,"CF")
grp_material_7=Checkbuttongroup(cadre_choice_material_1,"SE")
grp_material_3=Checkbuttongroup(cadre_choice_material_1,"RCC")
grp_material_5=Checkbuttongroup(cadre_choice_material_1,"CD")
grp_material_8=Checkbuttongroup(cadre_choice_material_2,"FS")
grp_material_9=Checkbuttongroup(cadre_choice_material_2,"COP")
grp_material_11=Checkbuttongroup(cadre_choice_material_2,"CR")
grp_material_12=Checkbuttongroup(cadre_choice_material_2,"AB")
grp_material_13=Checkbuttongroup(cadre_choice_material_2,"CT")
grp_material_14=Checkbuttongroup(cadre_choice_material_2,"LP")
grp_material_10=Checkbuttongroup(cadre_choice_material_2,"SD")
boutonexcel=Button(fenetre_choice_of_mat, text="TEST",width=15,height=1,bg="white",bd=5)
boutonexcel.pack()
fenetre_choice_of_mat.mainloop()
os.system("pause")
In this specific case, if you call pack on the button before any other widgets, and you set the side to "bottom", it will be centered at the bottom.
This is because the order in which pack is called matters. The packer works by allocating an entire side of the available space, and putting the widget on that side. The default side is "top", but setting it to "bottom" moves the widget to the bottom.
Once this button is on the bottom, that space is unavailable to any other widgets. Any other widgets that are subsequently packed must appear above it since that is where the unallocated space is.
Also, the default for the packer is to place the widget in the center of the space allocated for it. So, unless you specify otherwise the button will be in the middle of the bottom of the window.
boutonexcel=Button(fenetre_choice_of_mat, text="TEST",width=15,height=1,bg="white",bd=5)
boutonexcel.pack(side="bottom")
cadre_choice_material_1=Frame(fenetre_choice_of_mat,highlightbackground="blue", highlightthickness=2)
...
Ok I found by myself the solution.
Below is the code with the comments for the new lines. For a better readibility, I put "SOLUTION:" in the comment.
import os
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from tkinter import messagebox
fenetre_choice_of_mat=Tk()
fenetre_choice_of_mat.geometry("350x500")
class Checkbuttongroup:
def __init__(self, fenetre1, text1):
self.Checkbutton1 = IntVar()
self.texte=text1
self.fenetre12=fenetre1
Button1 = Checkbutton(self.fenetre12, text = self.texte,
variable = self.Checkbutton1,
onvalue = 1,
offvalue = 0,
height = 2,
width = 20,
anchor = "w")
Button1.pack()
bottom_frame=Frame(fenetre_choice_of_mat) #-- SOLUTION: I created a frame to put at the bottom
bottom_frame.pack(side='bottom')
cadre_choice_material_1=Frame(fenetre_choice_of_mat,highlightbackground="blue", highlightthickness=2)
cadre_choice_material_1.pack(side='left')
cadre_choice_material_2=Frame(fenetre_choice_of_mat,highlightbackground="blue", highlightthickness=2)
cadre_choice_material_2.pack(side='left')
grp_material_1=Checkbuttongroup(cadre_choice_material_1,"CW")
grp_material_2=Checkbuttongroup(cadre_choice_material_1,"Car Sling")
grp_material_4=Checkbuttongroup(cadre_choice_material_1,"Accessories electric")
grp_material_6=Checkbuttongroup(cadre_choice_material_1,"CW Filler")
grp_material_7=Checkbuttongroup(cadre_choice_material_1,"Shaft equipment")
grp_material_3=Checkbuttongroup(cadre_choice_material_1,"Rail for car and CW")
grp_material_5=Checkbuttongroup(cadre_choice_material_1,"Car doors")
grp_material_8=Checkbuttongroup(cadre_choice_material_2,"Floor selector")
grp_material_9=Checkbuttongroup(cadre_choice_material_2,"COP")
grp_material_11=Checkbuttongroup(cadre_choice_material_2,"Car")
grp_material_12=Checkbuttongroup(cadre_choice_material_2,"Adjustable bracket")
grp_material_13=Checkbuttongroup(cadre_choice_material_2,"Control")
grp_material_14=Checkbuttongroup(cadre_choice_material_2,"LOP")
grp_material_10=Checkbuttongroup(cadre_choice_material_2,"Shaft doors")
boutonexcel=Button(bottom_frame, text="TEST",width=15,height=1,bg="white",bd=5) #-- SOLUTION: I put the button in the bottom_frame
boutonexcel.pack(side='bottom')
fenetre_choice_of_mat.mainloop()
os.system("pause")
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 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 want to center a tkinter window on the screen, which can be done with:
root.geometry(f"+{(root.winfo_screenwidth()-root.winfo_width())//2}+"
f"{(root.winfo_screenheight()-root.winfo_height())//2}")
This is using the screen width and the width of the window to calculate the upper left corner. However, in order to find out the window width, I have to run root.update() as shown in the following example, which leads to the window showing up at a wrong position for a tiny moment.
import tkinter as tk
root = tk.Tk()
for i in range(20):
tk.Label(root, text='Hello World! '*5).pack()
# without the following line, the window dimensions are not being calculated
root.update()
root.geometry(f"+{(root.winfo_screenwidth()-root.winfo_width())//2}"
f"+{(root.winfo_screenheight()-root.winfo_height())//2}")
root.mainloop()
To avoid this, I can think of two solutions:
defining the window size in pixels, which means that the window size does not adjust automatically anymore, and
doing something like root.update() without the window being visible.
I don't know how to avoid the call to update(), but you could initially make the window completely transparent, which would prevent it from even momentarily showing up in the wrong position — thereby granting you the opportunity to position it properly and manually making it visible.
import tkinter as tk
root = tk.Tk()
# This changes the alpha value (how transparent the window should be).
# It ranges from 0.0 (completely transparent) to 1.0 (completely opaque).
root.attributes("-alpha", 0.0)
for i in range(20):
tk.Label(root, text='Hello World! '*5).pack()
tk.Button(root, text='Quit', command=root.quit).pack()
root.update() # Allow the window's size to be calculated,
# Move it so it's centered on the screen.
root.geometry(f"+{(root.winfo_screenwidth()-root.winfo_width())//2}"
f"+{(root.winfo_screenheight()-root.winfo_height())//2}")
# Now make it visible.
root.attributes("-alpha", 1.0)
root.mainloop()
The simplest way to achieve a clean window display is shown in the following code.
withdraw the master immediately, create widgets, update master before centering window and finally deiconify.
Works every time.
import tkinter as tk
def screencenter(o):
w, h = o.winfo_width(), o.winfo_height()
x = int((o.winfo_screenwidth() - w) / 2)
y = int((o.winfo_screenheight() - h) / 2)
o.geometry(f'{w}x{h}+{x}+{y}')
master = tk.Tk()
master.withdraw()
# create whatever widgets you need
master.update()
screencenter(master)
master.deiconify()
master.mainloop()
What you're looking for is root.withdraw() and root.deiconify().
The former will hide your window from view and the latter will show it.
I've included a full example below.
from tkinter import Tk
def show_it():
height = root.winfo_height()
width = root.winfo_width()
root.geometry(f"+{(s_width - width)//2}+"
f"{(s_height - height)//2}")
# show it again
root.deiconify()
root = Tk()
s_height = root.winfo_screenheight()
s_width = root.winfo_screenwidth()
root.withdraw()
# hide the window
root.after(200, show_it)
root.mainloop()