I am simply trying to center some buttons in Python and it won't work even after looking at other a dozen threads.
This is my code at the moment:
import tkinter as tk
HEIGHT = 600
WIDTH = 1000
root = tk.Tk()
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()
frame = tk.Frame(root, bg='#000A01')
frame.place(relwidth=1, relheight=1)
button0 = tk.Button(frame, text="LEFT", relief='flat', justify='center', bg='#000A01', fg='#00A010', padx='35', width='0', font=('Monofonto', 18))
button0.grid(row=0, column=0)
button1 = tk.Button(frame, text="CENTER", relief='flat', justify='center', bg='#000A01', fg='#00A010', padx='35', width='0', font=('Monofonto', 18))
button1.grid(row=0, column=1)
button2 = tk.Button(frame, text="RIGHT", relief='flat', justify='center', bg='#000A01', fg='#00A010', padx='35', width='0', font=('Monofonto', 18))
button2.grid(row=0, column=2)
root.mainloop()
this is the output
outputofabovecode
I want all buttons to be centered in the middle of the window and if I were to add more buttons in the same row it would add more to the center.
To center a button using place:
place(relx=0.5, rely=0.5, anchor=CENTER).
Related
The 2 buttons should take each of the half of the window, one on the left, one on the right. The height is fixed all time. With .grid() nor .place() I can come to that result. The red bar is the color of the frame where the buttons are placed on. The buttons resize in width with the window, but keep their constant height.
How to?
import tkinter as tk
root = tk.Tk()
frame = tk.Frame(root, bg='red')
frame.pack(fill='both', expand=True)
button1 = tk.Button(frame, text="<<")
button2 = tk.Button(frame, text=">>")
button1.grid(row=0, column=0, sticky='nsew')
button2.grid(row=0, column=1, sticky='nsew')
frame.columnconfigure(0, weight=1)
frame.columnconfigure(1, weight=1)
root.mainloop()
Thx.
In the mean time I got this:
import tkinter as tk
root = tk.Tk()
frame = tk.Frame(root, bg='red',height=30)
frame.pack(fill='both')
button1 = tk.Button(frame, text="<<")
button2 = tk.Button(frame, text=">>")
button1.place(relwidth=0.5, relx=0, relheight=1)
button2.place(relwidth=0.5, relx=0.5, relheight=1)
root.mainloop()
Assuming that the buttons are the only widgets in the frame (ie: you are making a toolbar), I would use pack. grid will also work, but it requires one extra line of code.
Using pack
Here's a version with pack. Notice that the frame is packed along the top and fills the window in the "x" direction. The buttons each are instructed to expand (ie: receive extra, unused space) and to fill the space allocated to them in the "x" direction.
import tkinter as tk
root = tk.Tk()
frame = tk.Frame(root, bg='red',height=30)
frame.pack(side="top", fill="x")
button1 = tk.Button(frame, text="<<")
button2 = tk.Button(frame, text=">>")
button1.pack(side="left", fill="x", expand=True)
button2.pack(side="right", fill="x", expand=True)
root.mainloop()
Using Grid
A version with grid is similar, but you must use columnconfigure to give a non-zero weight to the two columns:
import tkinter as tk
root = tk.Tk()
frame = tk.Frame(root, bg='red',height=30)
frame.pack(side="top", fill="x")
button1 = tk.Button(frame, text="<<")
button2 = tk.Button(frame, text=">>")
button1.grid(row=0, column=0, sticky="ew")
button2.grid(row=0, column=1, sticky="ew")
frame.grid_columnconfigure((0, 1), weight=1)
root.mainloop()
I have made a python gui using tkinter and i have been running it through the commandline. But when i tried to open it by double clicking, it shortly shows the window but exits again. I tried commenting out all the places where it uses images thinking that it is because it dosent have file-editing permissions when its run by double click and it worked! But i need to use images in the gui, anyone have solutions?
import tkinter as tk
from PIL import Image, ImageTk
import time
root = tk.Tk()
root.title("Krypto Coin")
root.iconbitmap('icon.ico')
root.configure(bg="#112233")
Frame = tk.Frame(root, width=1200, height=800, bg="#112233")
Frame.grid(columnspan=3, rowspan=5)
icon = Image.open('icon_round.png')
icon = ImageTk.PhotoImage(icon)
icon_label = tk.Label(image=icon, borderwidth=0, highlightthickness=0)
icon_label.place(relx=0.5, rely=0.2, anchor="center")
text = tk.Label(root, text="Welcome to Krypto Coin (KTC)", bg="#112233", fg="#ffffff", font=("Arial", 32))
text.place(relx=0.5, rely=0.45, anchor="center")
text = tk.Label(root, text="Enter id of the receiver and amount", bg="#112233", fg="#ffffff", font=("Arial", 24))
text.place(relx=0.5, rely=0.5, anchor="center")
def trasfer_button_pressed():
time.sleep(0.1)
trasfer_button_text.set("Loading...")
trasfer_button.configure(state="disable")
trasfer_button.configure(bg="#112233")
input_id.configure(state="disable")
input_amount.configure(state="disable")
def trasfer_button_enter(e):
if trasfer_button_text.get() == "Transfer":
trasfer_button.configure(bg="#334455")
def trasfer_button_leave(e):
if trasfer_button_text.get() == "Transfer":
trasfer_button.configure(bg="#223344")
trasfer_button_text = tk.StringVar()
trasfer_button = tk.Button(root, textvariable=trasfer_button_text, command=lambda:trasfer_button_pressed(), bg="#223344", fg="#ffffff", width=15, height=1, font=("Arial", 24), borderwidth=0, highlightthickness=0)
trasfer_button_text.set("Transfer")
trasfer_button.place(relx=0.5, rely=0.65, anchor="center")
trasfer_button.bind("<Enter>", trasfer_button_enter)
trasfer_button.bind("<Leave>", trasfer_button_leave)
input_id = tk.Entry(root, width=48, font = "Arial 14", bg="#334455", fg="#ffffff", borderwidth=0, highlightthickness=0)
input_id.insert(0, "ID")
input_id.place(relx=0.45, rely=0.8, anchor="center", height=30)
input_amount = tk.Entry(root, width=20, font = "Arial 14", bg="#334455", fg="#ffffff", borderwidth=0, highlightthickness=0)
input_amount.insert(0, "Amount")
input_amount.place(relx=0.65, rely=0.8, anchor="center", height=30)
root.mainloop()
The idea of this python program is to scrape recipes from a food website and output the recipes in a tkinter GUI when clicking the button. I have already applied the scrollbar to the GUI but it is not working as it should. I have already tried many ways but I did not succeed so I decided to ask help from the stack overflow community. I am new to tkinter so sorry for asking help for possibly a silly problem.
The code so far:
import tkinter as tk
from tkinter import Scrollbar
root = tk.Tk()
root.title('Italian-recipes')
height=600
width=895
canvas = tk.Canvas(root, height=height, width=width)
canvas.pack()
background_image = tk.PhotoImage(file='food.png')
background_label = tk.Label(root, image=background_image)
background_label.place(relwidth=1, relheight=1)
lower_frame = tk.Frame(root, bg='#80c1ff', bd=5)
lower_frame.place(relx=0.5, rely=0.25, relwidth=0.75, relheight=0.6, anchor='n')
frame = tk.Frame(root, bg='#AFF9A9', bd=5)
frame.place(relwidth=0.75, relheight=0.1, relx=0.5, rely=0.1, anchor='n')
button = tk.Button(frame, text='Enter food or ingredient', font=40, command=lambda:
test_function(entry.get()))
button.place(relx=0.55, relwidth=0.45, relheight=1)
entry = tk.Entry(frame, font=40)
entry.place(relwidth=0.5, relheight=1)
listbox = tk.Listbox(lower_frame, justify='left', bd=4)
listbox.place(relwidth=1, relheight=1)
label = tk.Label(listbox, anchor='nw', justify='left', bd=4)
label.place(relwidth=1, relheight=1)
scrollbar = Scrollbar(listbox, orient="vertical")
scrollbar.pack( side = 'right', fill = 'y' )
root.mainloop()
I did it with a textfield, that you can see how it work. Change it back to your listbox
import tkinter as tk
from tkinter import Scrollbar
root = tk.Tk()
root.title('Italian-recipes')
height=600
width=895
canvas = tk.Canvas(root, height=height, width=width)
canvas.pack()
lower_frame = tk.Frame(root, bg='#80c1ff', bd=5)
lower_frame.place(relx=0.5, rely=0.25, relwidth=0.75, relheight=0.6, anchor='n')
frame = tk.Frame(root, bg='#AFF9A9', bd=5)
frame.place(relwidth=0.75, relheight=0.1, relx=0.5, rely=0.1, anchor='n')
scrollbar = Scrollbar(lower_frame, orient="vertical")
scrollbar.pack( side = 'right', fill = 'y' )
listbox = tk.Text(lower_frame, yscrollcommand=scrollbar.set, bd=4)
listbox.pack(fill="both", expand="yes")
scrollbar.configure(command=listbox.yview)
root.mainloop()
The scroll don't work at all. What's wrong in the code?
I'm using Python 2.7.16
I read that listbox and text widgets are used only for text. As I want to use labels, I'm trying to insert the labels in a frame, but as a Frame didn't scroll I decided to use a canvas. But I couldn't get it to work.
from Tkinter import *
root = Tk()
frame = Frame(root, width=300, height=200)
frame.grid(row=0, column=0)
canvas=Canvas(frame, bg='#FFFFFF', width=300, height=200, scrollregion=(0,0,500,500))
vbar = Scrollbar(frame, orient=VERTICAL)
vbar.pack(side=RIGHT, fill=Y)
canvas.config(width=300, height=250)
canvas.pack(side=LEFT, expand=True, fill=BOTH)
mylist = Frame(canvas, width=100)
for x in range(10):
texto = Label(mylist, text='CODE', bd=2, width=7, relief=RIDGE)
texto.grid(row=x, column=0)
texto1 = Label(mylist, text='EQUIPAMENT', bd=2, width=20, relief=RIDGE)
texto1.grid(row=x, column=1)
mylist.place(x=0, y=0)
vbar.config(command=canvas.yview)
canvas.config(yscrollcommand=vbar.set)
mainloop()
I have created 4 frames, but when try to pack a label in my 4th frame, all frames get auto-resized, can anyone please have a look and advise what wrong thing I am doing. I am new to python and using python 3.6.
from tkinter import *
import time
localtime = time.asctime(time.localtime(time.time()))
root = Tk()
x = root.winfo_screenwidth()
y = root.winfo_screenheight()
root.geometry("%dx%d+0+0" %(x,y))
root.resizable(0,0)
frameinfo = Frame(root, width=x, height=100 , bg='powder blue', relief=SUNKEN)
frameinfo.pack(side=TOP,fill=BOTH, expand=YES)
framebutton = Frame(root, width=300, height=y-100, bg='dark grey', relief=SUNKEN)
framebutton.pack(side=LEFT,fill=BOTH, expand=YES)
frameradio = Frame(root, width=x-300, height=50, bg='light yellow', relief=SUNKEN)
frameradio.pack(side=TOP, fill=BOTH, expand=YES)
frameinput = Frame(root, width=x-300, height=y-150, bg='light green')
frameinput.pack(side=LEFT, fill=BOTH, expand=YES)
clock = Label(frameinfo, font =('aerial', 30, 'bold'), fg='blue', bg='powder blue')
clock.pack(side=RIGHT)
lblinfo = Label(frameinfo, text = "Cloning Tool", font =('aerial', 40, 'bold'), fg='black', bg='powder blue').pack()
def tick():
s =time.strftime('%d-%m-%y %H:%M:%S')
clock.config(text=s)
clock.after(200,tick)
tick()
butCapClone = Button(framebutton, text='Clone', borderwidth=5,width=20,height=2, font=9, command=showcapwindow).grid(row=0, padx=10, pady=20)
butexitClone = Button(framebutton, text='Exit', borderwidth=5,width=20,height=2, font=9, command=root.quit).grid(row=1, padx=10, pady=4)
modes = [('None','0'),('CAP','1'),('CAPTWO v1','2'),('CAPTWO v2','3'),]
radioclonetype = IntVar()
radioclonetype.set('0')
for text,mode in modes:
Radiobutton(frameradio, text=text, variable=radioclonetype, value=mode, font=('aerial',15), bg='light yellow', fg='purple').pack(side=LEFT)
labelprojectcode = Label(frameinput, text = "Project code(3 Characters")
labelprojectcode.pack(side=TOP)
# inputprojectcode = Entry(frameinput, fg='black', font=('aerial',15,'bold')).grid(row=0,column=1)
root.mainloop()
Frames expand or shrink based on the children widget size demand, given that there is extra space.Your label overfits my 1980x1080 resolution and that last frame's content becomes no longer visible. Comment out root.resizable(0,0) to see what happens:
#root.resizable(0,0)
This seems to be because pack works relative to sibling packs and the last called pack is given the minimum amount of space it requires while still fitting the options given to all packs. For example swap the position of:
frameradio.pack(side=TOP, fill=BOTH, expand=YES)
with:
frameinput.pack(side=LEFT, fill=BOTH, expand=YES)
You will get almost a completely new layout.