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()
Related
I am experimenting with Tkinter, as I was trying to place tree frames. Two frames must be placed side by side: the red one and the blue one. (see frame image). I place this two frames with this istructions:
redFrame = tk.Frame(master=masterFrame, bg='red')
redFrame.pack_propagate(0)
redFrame.pack(fill='both', side='left', expand='True')
blueFrame = tk.Frame(master=masterFrame, bg='blue')
destFrame.pack_propagate(0)
destFrame.pack(fill='both', side='right', expand='True')
Now I want to put another frame (green) inside the red one, placed on the left side of the main interface:
greenFrame = tk.Frame(master=masterFrame, width=100, height=100, bg='green')
greenFrame.pack_propagate(0)
greenFrame.pack(side='bottom', padx=0, pady=0)
The problem is that I can't choose its position. It still remain in the top side of the window and centered.
If I change the padx parameter nothing change.
If i change the pady parameter, the frame change its Y position. Why this not appened with the X posistion?
Widgets will by default be centered in their allocated space within the container.
Your code at the moment doesn't work, so I've changed it to this (I think it mimics what you're saying you currently have):
import tkinter as tk
root = tk.Tk()
root.geometry("600x400")
redFrame = tk.Frame(root, bg='red')
redFrame.pack_propagate(0)
redFrame.pack(fill='both', side='left', expand='True')
blueFrame = tk.Frame(root, bg='blue')
blueFrame.pack_propagate(0)
blueFrame.pack(fill='both', side='right', expand='True')
greenFrame = tk.Frame(redFrame, width=100, height=100, bg='green')
greenFrame.pack_propagate(0)
greenFrame.pack(side='top', padx=0, pady=0)
root.mainloop()
If you run this code, the greenFrame will appear as your image shows, at the top and centered inside redFrame.
If you want greenFrame to appear at the top left of redFrame, you can change the anchor point of the frame:
greenFrame = tk.Frame(redFrame, width=100, height=100, bg='green')
greenFrame.pack_propagate(0)
greenFrame.pack(side='top', padx=0, pady=0, anchor='w')
That is because when you do side="top" using pack, the widget gets allocated an entire strip of horizontal space. It gets centered within that space by default:
If you do side="left", then the widget gets allocated an entire strip of vertical space, and it gets centered in that space by default.
However in both cases you can change the anchor point, so that your component is placed differently within that allocated spaces.
This is the code which achieves what (I think) you want:
import tkinter as tk
root = tk.Tk()
root.geometry("600x400")
redFrame = tk.Frame(root, bg='red')
redFrame.pack_propagate(0)
redFrame.pack(fill='both', side='left', expand='True')
blueFrame = tk.Frame(root, bg='blue')
blueFrame.pack_propagate(0)
blueFrame.pack(fill='both', side='right', expand='True')
greenFrame = tk.Frame(redFrame, width=100, height=100, bg='green')
greenFrame.pack_propagate(0)
greenFrame.pack(side='top', padx=0, pady=0, anchor='w')
root.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 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.
I'm quite new to Tkinter but I have been practicing it for a bit now but whenever I try to move a label in a frame with grid geometry by specifying row and column, the label just stays in the middle.
In the code below, I try specifying the row and column for the name label which is in the top middle frame but it never moves. It just stays in the middle.
from tkinter import*
root=Tk()
frame_topleft = Frame(root, height=150, width=50, bg = "green")
frame_topmiddle = Frame(root, height=150, width=250, bg="red")
frame_topright = Frame(root, height=150, width=250, bg="green")
frame_bottomleft = Frame(root, height=300, width=50, bg="blue")
frame_bottommiddle = Frame(root, height=300, width=250, bg="yellow")
frame_bottomright = Frame(root, height=300, width=250, bg="blue")
label_name=Label(frame_topmiddle, text="Name", font="halvetica")
label_phone=Label(frame_topmiddle, text="Phone", font="halvetica")
frame_topmiddle.grid(row=0, column=1)
frame_topleft.grid(row=0, column=0)
frame_topright.grid(row=0, column=2)
frame_bottomleft.grid(row=1, column=0)
frame_bottommiddle.grid(row=1, column=1)
frame_bottomright.grid(row=1, column=2)
label_name.grid(row=0, column=0)
root.mainloop()
So, I'm just wondering how I can fix this. I want the name label at the top left of the top middle frame.
The frame_topmiddle shrinks to the size of label_name and is, by default, displayed at the center of (row 0, column 1).
If you set the sticky option of the grid method to 'nw':
frame_topmiddle.grid(row=0, column=1, sticky='nw')
then frame_topmiddle will be in the top left corner of (row 0, column 1) instead of the center.
If you want frame_topmiddle to keep its initial size, you need to do
frame_topmiddle.grid_propagate(False).
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)