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.
Related
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).
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()
Given the following grid layout configuration:
It was generated through code:
from Tkinter import *
master = Tk()
frame1 = Frame(master, width=100, height=100, bg="red")
frame1.grid(sticky="nsew", columnspan=4)
frame2 = Frame(master, width=100, height=100, bg="blue")
frame2.grid()
frame3 = Frame(master, width=100, height=100, bg="green")
frame3.grid(row=1, column=1)
frame4 = Frame(master, width=100, height=100, bg="yellow")
frame4.grid(row=1, column=2)
frame4 = Frame(master, width=100, height=100, bg="purple")
frame4.grid(row=1, column=3)
master.mainloop()
I tried to insert a Entry in frame1 so it extends to the whole frame1 width with the following code:
e1 = Entry(frame1)
e1.grid(sticky="we", columnspan=4)
However, I have got the following result:
How can I make the Entry widget occupy the same width as the frame1?
The entry is inside frame1, so the grid it is in is completely unrelated to the grid in the root window.
Since you didn't tell tkinter what to do with extra space inside frame1, it left it unused. If you want the entry widget which is in column 0 to fill the frame, you need to give its column a weight.
frame1.grid_columnconfigure(0, weight=1)
If this is the only widget going in frame1, you might want to consider using pack since you can do it all in one line of code:
e1.pack(side="top", fill="x", expand=True)
I have a canvas with an image I'd like to use for a background. My problem is that when I position the button to the correct place, and I try to scroll down, the button moves with the screen instead of staying where I want it to on the canvas.
frame = Frame(self)
frame.pack()
mapImg = PhotoImage(file='Fo4-pip-map.png')
canvas = Canvas(frame, width=2048, height=2048, scrollregion=(0,0,2048,2048))
canvas.create_image(0,0, image=mapImg, anchor='nw')
canvas.image = mapImg
xscrollbar = Scrollbar(frame, orient=HORIZONTAL)
xscrollbar.pack(side=BOTTOM, fill=X, anchor='s')
yscrollbar = Scrollbar(frame, orient=VERTICAL)
yscrollbar.pack(side=RIGHT, fill=Y, anchor='e')
xscrollbar.config(command=canvas.xview)
yscrollbar.config(command=canvas.yview)
canvas.config(xscrollcommand=xscrollbar.set, yscrollcommand=yscrollbar.set)
canvas.pack(side=LEFT, expand=True, fill=BOTH)
vaultImg = PhotoImage(file='Vault.png')
vaultImg = vaultImg.zoom(5)
vaultImg = vaultImg.subsample(32)
vault111Button = Button(canvas, width=30, height=30, borderwidth=0, image=vaultImg,
command=lambda: controller.show_frame('Vault111'))
vault111Button.image = vaultImg
vault111Button.place(x=150, y=100)
The place geometry manager places children at a fixed offset from their parent - in particular, the scrolling of a canvas doesn't apply to them. Get rid of that last line, and instead use:
canvas.create_window(150, 100, window=vault111Button)
Is it possible to get a text box to have a border even when inactive?
I am not sure I know what "border when inactive" means, but you can certainly add a border to a Text in TkInter. The following code creates two outer frames and two inner frames, then adds a Text to each inner frame. The frames are given a border of 5 px on all sides.
from Tkinter import *
root = Tk()
left_outer = Frame(root, bd=1)
left_outer.pack(side=LEFT, fill=Y, pady=5, padx=5)
right_outer = Frame(root, bd=1)
right_outer.pack(side=LEFT, fill=Y, pady=5, padx=5)
left = Frame(left_outer, bd=2, relief=SUNKEN)
right = Frame(right_outer, bd=2, relief=SUNKEN)
left.pack(fill=Y)
right.pack(fill=Y)
t_start = Text(left, width=20, height=200)
t_start.pack(side=LEFT, fill=Y)
s_start = Scrollbar(left)
s_start.pack(side=RIGHT, fill=Y)
s_start.config(command=t_start.yview)
t_start.config(yscrollcommand=s_start.set)
t_end = Text(right, width=20, height=200)
t_end.pack(side=LEFT, fill=Y)
s_end = Scrollbar(right)
s_end.pack(side=RIGHT, fill=Y)
s_end.config(command=t_end.yview)
t_end.config(yscrollcommand=s_end.set)
root.geometry("400x200")
root.mainloop()
odie5533's answer covers giving a border to a frame that contains only a Text object. This is a great way to give a 2D around a text object, but adds another widget in the mix. I think the original question was related to setting both the border width and relief type of the Text object. This snippet gives a relief to the Text object without involving another frame.
from Tkinter import *
root = Tk()
text_top = Text(root, relief=GROOVE, height=5, width = 40, borderwidth=2)
text_top.pack()
text_bottom = Text(root, relief=RIDGE, height=5, width = 40, borderwidth=2)
text_bottom.pack()
root.geometry("400x200")
root.mainloop()