I am new to this so not sure where I am going wrong here. I want to make my Frame_1 stick to the four corners of the window as you drag it out from the bottom right hand corner.
from tkinter import *
from tkinter import scrolledtext
from tkinter import ttk
window = Tk()
window.title("My Program")
tab_control = ttk.Notebook(window)
tab1 = ttk.Frame(tab_control)
tab1.grid(row=0, column=0)
tab2 = ttk.Frame(tab_control)
tab2.grid(row=0, column=0)
tab_control.grid(row=0, column=0, sticky=NSEW)
tab_control.add(tab1, text='First')
tab_control.add(tab2, text='Second')
labe1frame_1 = LabelFrame(tab1, text="Frame_1")
labe1frame_1.grid(row=0, column=0, padx=10, pady=10, sticky=NSEW)
txtbox = scrolledtext.ScrolledText(labe1frame_1, width=40, height=10)
txtbox.grid(row=0, column=0)
window.rowconfigure(0, weight=1)
window.columnconfigure(0, weight=1)
labe1frame_1.rowconfigure(0, weight=1)
labe1frame_1.columnconfigure(0, weight=1)
window.mainloop()
In your current GUI set up, using pack throughout may be a better idea:
import tkinter as tk
from tkinter import scrolledtext
from tkinter import ttk
if __name__ == '__main__':
window = tk.Tk()
window.title("My Program")
tab_control = ttk.Notebook(window)
tab1 = tk.Frame(tab_control)
tab1.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
tab2 = tk.Frame(tab_control)
tab2.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
tab_control.pack(fill=tk.BOTH, expand=True)
tab_control.add(tab1, text='First')
tab_control.add(tab2, text='Second')
labe1frame_1 = tk.LabelFrame(tab1, text="Frame_1")
labe1frame_1.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
txtbox = scrolledtext.ScrolledText(labe1frame_1, width=40, height=10)
txtbox.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
window.mainloop()
You can pack your Labeframe and scrolledText with this command to achieve that pack(expand=True, fil=BOTH) by removing the grid geometry layout manager.
from tkinter import *
from tkinter import scrolledtext
from tkinter import ttk
window = Tk()
window.title("My Program")
tab_control = ttk.Notebook(window)
tab1 = ttk.Frame(tab_control)
tab1.grid(row=0, column=0)
tab2 = ttk.Frame(tab_control)
tab2.grid(row=0, column=0, sticky=NSEW)
tab_control.grid(row=0, column=0, columnspan=3, padx=10, pady=10, sticky=E+W+N+S)
tab_control.add(tab1, text='First')
tab_control.add(tab2, text='Second')
labe1frame_1 = LabelFrame(tab1, text="Frame_1")
labe1frame_1.pack(expand=True, fil=BOTH)
txtbox = scrolledtext.ScrolledText(labe1frame_1, width=40, height=10)
txtbox.pack(expand=True, fil=BOTH)
window.rowconfigure(0, weight=1)
window.columnconfigure(0, weight=1)
labe1frame_1.rowconfigure(0, weight=1)
labe1frame_1.columnconfigure(0, weight=1)
window.mainloop()
You can allow your textbox and Frame 1 to expand by adding a weight to tab1 and a sticky to textbox.
When using grid() you will want to use columnconfig() and rowconfig() to provide weights to that frame so it can expand with the window resizing.
For the textbox to expand with the frame you will need to add the sticky argument also like this:
txtbox.grid(row=0, column=0, sticky="nswe")
See below code.
from tkinter import *
from tkinter import scrolledtext
from tkinter import ttk
window = Tk()
window.title("My Program")
tab_control = ttk.Notebook(window)
tab1 = ttk.Frame(tab_control)
tab1.grid(row=0, column=0)
tab1.columnconfigure(0, weight=1) # added weight
tab1.rowconfigure(0, weight=1) # added weight
tab2 = ttk.Frame(tab_control)
tab2.grid(row=0, column=0)
tab_control.grid(row=0, column=0, sticky="nswe")
tab_control.add(tab1, text='First')
tab_control.add(tab2, text='Second')
labe1frame_1 = LabelFrame(tab1, text="Frame_1")
labe1frame_1.grid(row=0, column=0, padx=10, pady=10, sticky="nswe")
txtbox = scrolledtext.ScrolledText(labe1frame_1, width=40, height=10)
txtbox.grid(row=0, column=0, sticky="nswe") # added sticky
window.rowconfigure(0, weight=1)
window.columnconfigure(0, weight=1)
labe1frame_1.rowconfigure(0, weight=1)
labe1frame_1.columnconfigure(0, weight=1)
window.mainloop()
The width of a grid column inside a given widget will be equal to the width of its widest cell, and the height of a grid row will be the height of its tallest cell. The sticky attribute on a widget controls only where it will be placed if it doesn't completely fill the cell.
from tkinter import *
from tkinter import scrolledtext
from tkinter import ttk
window = Tk()
window.title("My Program")
tab_control = ttk.Notebook(window)
tab1 = ttk.Frame(tab_control)
tab1.grid(row=0, column=0, sticky=NSEW) #add sticky option
tab2 = ttk.Frame(tab_control)
tab2.grid(row=0, column=0)
tab_control.grid(row=0, column=0, sticky=NSEW)
tab_control.add(tab1, text='First')
tab_control.add(tab2, text='Second')
labe1frame_1 = LabelFrame(tab1, text="Frame_1")
labe1frame_1.grid(row=0, column=0, padx=10, pady=10, sticky=NSEW)
txtbox = scrolledtext.ScrolledText(labe1frame_1, width=40, height=10)
txtbox.grid(row=0, column=0, sticky=NSEW) #add sticky option
window.rowconfigure(0, weight=1)
window.columnconfigure(0, weight=1)
labe1frame_1.rowconfigure(0, weight=1)
labe1frame_1.columnconfigure(0, weight=1)
#configure the row and column size of parent window
tab1.columnconfigure(0,weight=3)
tab1.rowconfigure(0,weight=3)
window.mainloop()
Related
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
left_frame_1 = tk.Frame(root, background="#ff0000")
left_frame_1.grid(row=0, column=0)
left_frame_2 = tk.Frame(left_frame_1)
left_frame_2.grid(row=0, column=0)
left_label_1 = tk.Label(left_frame_2, text="HELLO")
left_label_2 = tk.Label(left_frame_2, text="WORLD")
left_label_3 = tk.Label(left_frame_2, text="=D")
left_label_1.grid(row=0, column=0)
left_label_2.grid(row=1, column=0)
left_label_3.grid(row=2, column=0)
right_frame1 = tk.Frame(root, background="#00ff00")
right_frame1.grid(row=0, column=1, sticky="nsew")
right_frame_2 = tk.Frame(right_frame1, background="#0000ff")
right_frame_2.grid(row=0, column=0)
right_label_1 = tk.Label(right_frame_2, text="CENTER ME!")
right_label_1.grid(row=0, column=0)
root.mainloop()
When my parent frame expands to all its free space, the child frame doesn't, instead it just stays on top.
I've been testing if .grid() has something to do with it, but haven't found anything.
Even if I add sticky="nsew" to both the frame and the label, there is still no change.
right_frame1 = tk.Frame(root, background="#00ff00")
right_frame1.grid(row=0, column=1, sticky="nsew")
right_frame_2 = tk.Frame(right_frame1, background="#0000ff")
right_frame_2.grid(row=0, column=0, sticky="nsew")
right_label_1 = tk.Label(right_frame_2, text="CENTER ME!")
right_label_1.grid(row=0, column=0, sticky="nsew")
My goal is for the parent frame (the one with the green color) to expand to all available space (which I've achieved), and for the child frame containing the label to expand.
right_frame_2 looks because it expands.
right_frame_1 is not visible because it is completely covered by right_frame_2.
I hope your help, thank you.
To get the result of the last image in the question, you need to:
change sticky options of .grid() for right_frame_2 and right_label_1
set weight options of .rowconfigure() and .columnconfigure() on root, right_frame1 and right_frame_2
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
left_frame_1 = tk.Frame(root, background="#ff0000")
left_frame_1.grid(row=0, column=0)
left_frame_2 = tk.Frame(left_frame_1)
left_frame_2.grid(row=0, column=0)
left_label_1 = tk.Label(left_frame_2, text="HELLO")
left_label_2 = tk.Label(left_frame_2, text="WORLD")
left_label_3 = tk.Label(left_frame_2, text="=D")
left_label_1.grid(row=0, column=0)
left_label_2.grid(row=1, column=0)
left_label_3.grid(row=2, column=0)
right_frame1 = tk.Frame(root, background="#00ff00")
right_frame1.grid(row=0, column=1, sticky="nsew")
right_frame_2 = tk.Frame(right_frame1, background="#0000ff")
right_frame_2.grid(row=0, column=0, sticky="nsew") # expand to fill available space
right_label_1 = tk.Label(right_frame_2, text="CENTER ME!")
right_label_1.grid(row=0, column=0, sticky="ew") # expand horizontally
root.rowconfigure(0, weight=1) # make left and right frame expand vertically
root.columnconfigure(1, weight=1) # make right frame expand horizontally
# allocate all space to right_frame_2
right_frame1.rowconfigure(0, weight=1)
right_frame1.columnconfigure(0, weight=1)
# allocate all space of right_frame_2 to right_label_1
right_frame_2.rowconfigure(0, weight=1)
right_frame_2.columnconfigure(0, weight=1)
root.mainloop()
Result:
When the window is resized:
I have created a GUI for reading a PDF file. As soon as I load PDF, the grid expands on x axis.
I have tried setting weights of row and column as 1, but I have no clue what else I am missing.
I want to restrict these 2 LabelFrame from resizing in wrong proportion, even if I resize the window, the proportions should be same.
In short, I want the two LabelFrame to be in equal space!
Does anyone have any idea how to achieve so?
My code
import cv2
from PIL.ImageTk import PhotoImage
from pdf2image import convert_from_path
from tkinter.ttk import Separator
from tkinter import Tk, LabelFrame, Label, Frame, Button
from tkinter.filedialog import askopenfilename
class GUI(Tk):
def __init__(self):
super().__init__()
self.geometry('1000x600')
# self.resizable(False, False)
self.init_frame()
def open_file(self):
file_path = askopenfilename(
title='Select a PDF...',
filetypes=(('PDF', '*.PDF'),))
h = self.pdf_frame.winfo_height()
w = self.pdf_frame.winfo_width()
print(h, w)
img = PhotoImage(convert_from_path(file_path, size=(w, h))[0])
self.pdf_frame.configure(image=img)
self.pdf_frame.image = img
def clear_file(self):
self.init_pdf_frame()
def init_frame(self):
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
self.frame = Frame(self)
self.frame.rowconfigure(0, weight=1)
self.frame.columnconfigure(0, weight=1)
self.frame.columnconfigure(1, weight=1)
self.frame.grid(column=0, row=0, sticky='news', padx=10, pady=10)
self.init_pdf_frame()
self.init_ocr_frame()
def init_pdf_frame(self):
image_viewer = LabelFrame(self.frame, text="PDF")
image_viewer.rowconfigure(2, weight=1)
image_viewer.columnconfigure(0, weight=1)
image_viewer.grid(column=0, row=0, sticky='news')
options = Frame(image_viewer)
options.columnconfigure(0, weight=1)
options.columnconfigure(1, weight=1)
options.grid(column=0, row=0, padx=5, pady=10, sticky='news')
open_button = Button(options, text='Open', command=self.open_file)
open_button.grid(column=0, row=0, padx=10, sticky='news')
clear_button = Button(options, text='Clear', command=self.clear_file)
clear_button.grid(column=1, row=0, padx=10, sticky='news')
line = Separator(image_viewer, orient='horizontal')
line.grid(row=1, column=0, sticky='ew')
self.pdf_frame = Label(image_viewer, text="PDF result appears here")
self.pdf_frame.grid(row=2, column=0, sticky='news')
def init_ocr_frame(self):
ocr_label = LabelFrame(self.frame, text="OCR")
ocr_label.rowconfigure(0, weight=1)
ocr_label.columnconfigure(0, weight=1)
ocr_label.grid(column=1, row=0, sticky='news')
label = Label(ocr_label, text="PDF result appears here")
label.grid(row=0, column=0, padx=10, pady=10, sticky='news')
if __name__ == '__main__':
GUI().mainloop()
I was wondering why when I use the columnspan parameter in the .grid() function the button does not change size. Should it not span all columns within the frame? Below I have my code and the output. Thanks all.
# Import Modules
from tkinter import ttk
import tkinter as tk
import os
# Basic Structure
root = tk.Tk()
root.minsize(600, 700)
root.maxsize(600, 700)
root.title("Training HUB")
root.config(bg="slategray")
# Create Frames - Title, Left and Right Frames
title_frame = tk.Frame(root, width=580, height=50, bg='white', highlightbackground="black", highlightthickness=1)
title_frame.grid(row=0, columnspan=2, padx=10, pady=5)
left_frame = tk.Frame(root, width=280, height=550, bg='white', highlightbackground="black", highlightthickness=1)
left_frame.grid(row=1, column=0, padx=10, pady=5)
left_frame.grid_propagate(0)
right_frame = tk.Frame(root, width=280, height=550, bg='white', highlightbackground="black", highlightthickness=1)
right_frame.grid(row=1, column=1, padx=10, pady=5)
right_frame.grid_propagate(0)
# Buttons
button1 = ttk.Button(right_frame, text="Run Processing").grid(
row=0, column=0, columnspan = 2, padx=5, pady=5, sticky = 'ew')
# Run Application
root.mainloop()
Tkinter Output:
I have a layout with Frame and pack some widwets inside. This works so fare. But for the treeview widget it doesn't work and gives a strange error:
Display Names in the Treeview doesn't work with pack tkinter.TclError:
cannot use geometry manager pack inside . which already has slaves
managed by grid
Need help from experienced tkinter user.
Here my layout:
here, if I try the same with a treeview instead of a label widget:
here is my program:
import tkinter as tk
from tkinter import ttk
# Main App
win = tk.Tk()
win.title('Layout Test')
win.geometry('1200x720+300+300')
win.resizable(True, True)
# Frame Design
top_frame = tk.Frame(win, background="#FFF0C1", bd=1, relief="sunken")
left_frame = tk.Frame(win, background="#D2E2FB", bd=1, relief="sunken")
center_frame = tk.Frame(win, background="#CCE4CA", bd=1, relief="sunken")
right_frame = tk.Frame(win, background ='lightblue', bd=1, relief='sunken')
bottom_frame = tk.Frame(win, background="#F5C2C1", bd=1, relief="sunken")
top_frame.grid(row=0, column=0, columnspan=3, sticky="nsew", padx=2, pady=2)
left_frame.grid(row=1, column=0, sticky="nsew", padx=2, pady=2)
center_frame.grid(row=1, column=1, sticky="nsew", padx=2, pady=2)
right_frame.grid(row=1, column=2, sticky="nsew", padx=2, pady=2)
bottom_frame.grid(row=3, column=0, columnspan=3, sticky="nsew", padx=2, pady=2)
win.grid_rowconfigure(0, weight=6)
win.grid_rowconfigure(1, weight=40)
win.grid_rowconfigure(3, weight=1)
win.grid_columnconfigure(0, weight=1)
win.grid_columnconfigure(1, weight=2)
win.grid_columnconfigure(2, weight=4)
# Display Names in the Treeview doesn't work with pack
# tkinter.TclError: cannot use geometry manager pack inside .
# which already has slaves managed by grid
treeview = ttk.Treeview(win)
treeview.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)
treeview.insert('center_frame','0','item1', text = 'First item',tag='T' )
treeview.insert('center_frame','1','item2', text = 'Second item',tag='T' )
treeview.insert('center_frame','2','item3', text = 'Third item',tag='T' )
treeview.insert('center_frame','3','item4', text = 'Forth item',tag='T' )
treeview.insert('center_frame','end','item5', text = 'Five item',tag='T' )
treeview.insert('item1','end','item6', text = 'Sechster Text',tag='T' )
treeview.tag_configure('T', font=('Calibre', 15))
"""
# Test works well with pack
MyLabel = tk.Label(center_frame,text="Label inside Frame1")
MyLabel.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)
"""
# Program here
# Status Bar at bottom_frame works well with pack
statustext = tk.StringVar()
statustext.set(' ... choose your avm_xml file') # will be changed from file dialoge
status = ttk.Label(bottom_frame, textvariable=statustext, borderwidth='25', relief=tk.SUNKEN, anchor=tk.W)
status.pack(side=tk.BOTTOM, fill=tk.X) #fill='both', expand=False, padx=8, pady=8
win.mainloop()
treeview has win as master, so when you try to pack it you get an error since you used grid for the other widgets in win. This is because the layout managers pack and grid cannot be used simultaneously in the same master widget.
On the other hand, your test label's master is center_frame, so you can pack it inside. If you change your treeview's master to center_frame, you will be able to pack it like the label.
import tkinter as tk
from tkinter import ttk
# Main App
win = tk.Tk()
win.title('Layout Test')
win.geometry('1200x720+300+300')
win.resizable(True, True)
# Frame Design
top_frame = tk.Frame(win, background="#FFF0C1", bd=1, relief="sunken")
left_frame = tk.Frame(win, background="#D2E2FB", bd=1, relief="sunken")
center_frame = tk.Frame(win, background="#CCE4CA", bd=1, relief="sunken")
right_frame = tk.Frame(win, background ='lightblue', bd=1, relief='sunken')
bottom_frame = tk.Frame(win, background="#F5C2C1", bd=1, relief="sunken")
top_frame.grid(row=0, column=0, columnspan=3, sticky="nsew", padx=2, pady=2)
left_frame.grid(row=1, column=0, sticky="nsew", padx=2, pady=2)
center_frame.grid(row=1, column=1, sticky="nsew", padx=2, pady=2)
right_frame.grid(row=1, column=2, sticky="nsew", padx=2, pady=2)
bottom_frame.grid(row=3, column=0, columnspan=3, sticky="nsew", padx=2, pady=2)
win.grid_rowconfigure(0, weight=6)
win.grid_rowconfigure(1, weight=40)
win.grid_rowconfigure(3, weight=1)
win.grid_columnconfigure(0, weight=1)
win.grid_columnconfigure(1, weight=2)
win.grid_columnconfigure(2, weight=4)
treeview = ttk.Treeview(center_frame) # <-- changed master from win to center_frame
treeview.pack(side=tk.LEFT, fill=tk.BOTH, expand=1) # <-- pack works now
# Program here
# Status Bar at bottom_frame works well with pack
statustext = tk.StringVar()
statustext.set(' ... choose your avm_xml file') # will be changed from file dialoge
status = ttk.Label(bottom_frame, textvariable=statustext, borderwidth='25', relief=tk.SUNKEN, anchor=tk.W)
status.pack(side=tk.BOTTOM, fill=tk.X) #fill='both', expand=False, padx=8, pady=8
win.mainloop()
As far as I am concerned, if you change treeview.pack() to treeview.grid(sticky=W)etc it might work. You can't have both pack and grid in the same widget.
I have a child window (root) in Tkinter. When i open the new window for import data and processing, this child window go under the main window.
I used
root.lift()
root.call('wm', 'attributes', '.', '-topmost', True)
but the child window remain in the top always, also when a directory window is open
from __future__ import division
from Tkinter import *
from math import pi
import tkMessageBox
import Tkinter, Tkconstants, tkFileDialog
from numpy import nan
import Tkinter as tk
import os, shutil
class MainWindow(Frame):
def __init__(self):
Frame.__init__(self)
self.filename = None
self.master.title("input")
self.master.minsize(250, 150)
self.grid(sticky=E+W+N+S)
top=self.winfo_toplevel()
top.rowconfigure(0, weight=1)
top.columnconfigure(0, weight=1)
for i in range(1): self.rowconfigure(i, weight=1)
self.columnconfigure(1, weight=1)
self.button0 = Button(self, text="start", command=self.open_new_window, activeforeground="red")
self.button0.grid(row=0, column=1, pady=2, padx=2, sticky=E+W+N+S)
def open_new_window(self):
root = tk.Toplevel(self)
root.title("process")
root.minsize(400, 150)
root.maxsize(400, 150)
root.grid()
top = root.winfo_toplevel()
top.rowconfigure(0, weight=1)
top.columnconfigure(0, weight=1)
root.CHART_FILE_TYPES = [('All files', '*'),('text file', '.txt')]
for i in range(5): root.rowconfigure(i, weight=1)
root.columnconfigure(1, weight=1)
root.CheckVar_a = IntVar()
root.check_a = tk.Checkbutton(root, text="A", variable=root.CheckVar_a, onvalue=0, offvalue=1)
root.check_a.grid(row=0, column=0, pady=0, padx=0, columnspan=3, sticky=W)
root.CheckVar_b = IntVar()
root.check_b = tk.Checkbutton(root, text="B", variable=root.CheckVar_b, onvalue=0, offvalue=1)
root.check_b.grid(row=1, column=0, pady=0, padx=0, columnspan=3, sticky=W)
root.CheckVar_c = IntVar()
root.check_c = tk.Checkbutton(root, text="C", variable=root.CheckVar_c, onvalue=0, offvalue=1)
root.check_c.grid(row=2, column=0, pady=0, padx=0, columnspan=3, sticky=W)
root.CheckVar_d = IntVar()
root.check_d = tk.Checkbutton(root, text="D", variable=root.CheckVar_d, onvalue=0, offvalue=1)
root.check_d.grid(row=3, column=0, pady=0, padx=0, columnspan=3, sticky=W)
def open_file():
root.filename = tkFileDialog.askopenfilename(filetypes=[("Text Files",'.txt')])
if not root.filename:
tkMessageBox.showerror("Error", message="No text file (*.txt) imported ")
return root.filename
open_txt = tk.Button(root, text="Open", command=open_file, activeforeground="red")
open_txt.grid(row=4, column=0, pady=2, padx=2, sticky=E+W+N+S)
def process():
if not root.filename:
tkMessageBox.showerror("Error", message="None file (*.txt) active to process")
return None
pass
pro = tk.Button(root, text="Process", command=process, activeforeground="red")
pro.grid(row=4, column=1, pady=2, padx=2, sticky=E+W+N+S)
if __name__=="__main__":
d = MainWindow()
d.mainloop()
You are using tkinter wrong. You can't have more than one instance of Tk in your program. You get one implicitly when you create a frame before creating the root window, then you get another in open_new_window.
To create a child window, create an instance of Toplevel.