How can I put a ttk.LabelFrame inside a ttk.PanedWindow? - python

I'm initiating in ttk and I keep getting this error while trying to add a LabelFrame inside a PanedWindow. Happens on line 46 (self.pwEntries.add(self.lfEntries)):
_tkinter.TclError: wrong # args: should be ".!frame.!panedwindow.!panedwindow add window"
As I've no idea how to solve it I would really appreciate some help, please...
My code goes like this:
from tkinter import *
from tkinter import ttk
class CalcVendGame:
def __init__(self, root):
root.title("Calcula Venda Games")
self.mainframe = ttk.Frame(root, padding="3 3 12 12")
self.mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
self.pwRoot = ttk.PanedWindow(self.mainframe, orient=VERTICAL)
self.pwEntries = ttk.PanedWindow(self.pwRoot, orient=HORIZONTAL)
self.lfEntries = ttk.LabelFrame(self.pwEntries, labelanchor='nw').grid(
column=0, row=0, sticky=(N, W))
ttk.Label(self.lfEntries, text="Valor em Dólar de cada cópia:", padding="5").grid(
column=2, row=1, sticky=E)
self.CopiaEmDolar = StringVar()
cpEmDol_entry = ttk.Entry(
self.lfEntries, width=7, textvariable=self.CopiaEmDolar)
cpEmDol_entry.grid(column=3, row=1, sticky=W)
ttk.Label(self.lfEntries, text="Ex: \"19.99\"", padding="5").grid(
column=4, row=1, sticky=W)
ttk.Label(self.lfEntries, text="Quantidade de cópias vendidas:", padding="5").grid(
column=2, row=2, sticky=E)
self.CopiasVendidas = StringVar()
cpVend_entry = ttk.Entry(
self.lfEntries, width=7, textvariable=self.CopiasVendidas)
cpVend_entry.grid(column=3, row=2, sticky=W)
ttk.Button(self.lfEntries, text="Calcular").grid(
column=4, row=3, sticky=W)
self.pwEntries.add(self.lfEntries)
self.pwPC = ttk.PanedWindow(self.pwRoot, orient=HORIZONTAL)
self.pwSteam = ttk.PanedWindow(self.pwPC, orient=VERTICAL)
self.lfSteam = ttk.LabelFrame(self.pwSteam, padding=(
5), text='Steam', labelanchor='nw')
ttk.Label(self.lfSteam, text="Cut 35%", padding="5").grid(
column=0, row=0, sticky=W)
ttk.Separator(self.lfSteam, orient=HORIZONTAL)
self.pwPatreon = ttk.PanedWindow(self.pwRoot, orient=VERTICAL)
self.pwPC.add(self.pwSteam)
self.pwPC.add(self.pwPatreon)
self.pwMobile = ttk.PanedWindow(self.pwRoot, orient=HORIZONTAL)
# self.pwRoot.add(self.lfEntries)
self.pwRoot.add(self.pwEntries)
self.pwRoot.add(self.pwPC)
self.pwRoot.add(self.pwMobile)
root = Tk()
CalcVendGame(root)
root.mainloop()
Also, this code give me no errors but results on a blank window:
from tkinter import *
from tkinter import ttk
master = Tk()
mainframe = ttk.Frame(master, padding="3 3 10 10")
p = ttk.Panedwindow(mainframe, orient=VERTICAL)
# two panes, each of which would get widgets gridded into it:
f1 = ttk.Labelframe(p, text='Pane1', width=100, height=100)
ttk.Label(f1, text="Valor em Dólar de cada cópia:", padding="5")
f2 = ttk.Labelframe(p, text='Pane2', width=100, height=100)
ttk.Label(f2, text="Quantidade de cópias vendidas:", padding="5")
p.add(f1)
p.add(f2)
master.mainloop()

You forgot to use any layout manager on the widgets. Below is an updated code using pack():
from tkinter import *
from tkinter import ttk
master = Tk()
mainframe = ttk.Frame(master, padding="3 3 10 10")
mainframe.pack()
p = ttk.Panedwindow(mainframe, orient=VERTICAL)
p.pack()
# two panes, each of which would get widgets gridded into it:
f1 = ttk.Labelframe(p, text='Pane1', width=100, height=100)
ttk.Label(f1, text="Valor em Dólar de cada cópia:", padding="5").pack()
f2 = ttk.Labelframe(p, text='Pane2', width=100, height=100)
ttk.Label(f2, text="Quantidade de cópias vendidas:", padding="5").pack()
p.add(f1)
p.add(f2)
master.mainloop()

Related

layout buttons within frame nested in tkk.Notebook

I can't properly layout buttons within frame nested in tkk.Notebook
In Main.py I create ttk.Notebook and attach mainTab instance
root = tk.Tk()
rootFrame = tk.Frame(root, width=600, height=300)
rootFrame.grid(columnspan=1, rowspan=2)
rootFrame.pack(expand=1, fill="both")
tabs = ttk.Notebook(rootFrame)
tabs.grid(column=0, row=1, columnspan=1, rowspan=1)
mainTab = ttk.Frame(tabs)
mainTab.grid(columnspan=3, rowspan=6)
tabs.add(mainTab, text="Main")
rootFrame.pack(expand=1, fill="both")
mainPane = MainTab(root,mainTab)
root.mainloop()
in mainTab.py I'm trying to insert buttonFrame and layout two buttons within it
class MainTab:
...
def __init__(self, root, mainTab) -> None:
self.root = root
buttonFrame = tk.Frame(mainTab, bg="white")
buttonFrame.grid(column=0, row=4, columnspan=3, rowspan=1)
self.start_btn = tk.Button(buttonFrame, text="Start", command=lambda:self.start_timer(), font=BUTTON_FONT, bg="green", fg="white") # , height=1, width=14
self.start_btn.grid(column=0, row=0, columnspan=2)
self.reset_btn = tk.Button(buttonFrame, text="X", command=lambda:self.reset_timer(), font=BUTTON_FONT, bg="green", fg="white") # , height=1, width=1
self.reset_btn.grid(column=2, row=0)
...
As a result start button is not properly placed in the grid
It looks like buttonFrame takes full parent frame width and buttons placed in the middle regardless of their grid settings.
How I can properly layout buttons within buttonFrame?
Here is the requested "minimal reproducible example" which also look not good
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title("Time Tracker")
root.iconbitmap('./assets/logoTransp4icon24.ico')
rootFrame = tk.Frame(root, width=600, height=300)
rootFrame.grid(columnspan=1, rowspan=2)
rootFrame.pack(expand=1, fill="both")
tabs = ttk.Notebook(rootFrame)
tabs.grid(column=0, row=1, columnspan=1, rowspan=1)
mainTab = ttk.Frame(tabs)
mainTab.grid(columnspan=3, rowspan=6)
buttonFrame = tk.Frame(mainTab, bg="white")
buttonFrame.grid(column=0, row=4, columnspan=3, rowspan=1)
start_btn = tk.Button(buttonFrame, text="Start", command=lambda:self.start_timer(), font="Arial", bg="green", fg="white") # , height=1, width=14
start_btn.grid(column=0, row=0, columnspan=2)
reset_btn = tk.Button(buttonFrame, text="X", command=lambda:self.reset_timer(), font="Arial", bg="green", fg="white") # , height=1, width=1
reset_btn.grid(column=2, row=0)
timerDisplay = tk.Label(mainTab, text="00:00:00", font="Arial")
timerDisplay.grid(columnspan=2, column=1, row=4)
tabs.add(mainTab, text="Main")
rootFrame.pack(expand=1, fill="both")
root.mainloop()
buttonFrame occupies column 0 to 2 and timerDisplay occupies column 1 to 2. So timerDisplay overlaps buttonFrame. Removing columnspan=3 in buttonFrame.grid(...) can fix the overlapping issue:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title("Time Tracker")
root.iconbitmap('./assets/logoTransp4icon24.ico')
rootFrame = tk.Frame(root, width=600, height=300)
#rootFrame.grid(columnspan=1, rowspan=2) # override by below line
rootFrame.pack(expand=1, fill="both")
tabs = ttk.Notebook(rootFrame)
tabs.grid(column=0, row=1, rowspan=1)
mainTab = ttk.Frame(tabs)
#mainTab.grid(columnspan=3, rowspan=6) # not necessary
buttonFrame = tk.Frame(mainTab, bg="white")
buttonFrame.grid(column=0, row=4, rowspan=1) # removed columnspan=3
start_btn = tk.Button(buttonFrame, text="Start", command=lambda:self.start_timer(), font="Arial", bg="green", fg="white") # , height=1, width=14
start_btn.grid(column=0, row=0, columnspan=2)
reset_btn = tk.Button(buttonFrame, text="X", command=lambda:self.reset_timer(), font="Arial", bg="green", fg="white") # , height=1, width=1
reset_btn.grid(column=2, row=0)
timerDisplay = tk.Label(mainTab, text="00:00:00", font="Arial")
timerDisplay.grid(columnspan=2, column=1, row=4)
tabs.add(mainTab, text="Main")
#rootFrame.pack(expand=1, fill="both") # already called above
root.mainloop()

Python: Costing calculator output

Good day all
I'm busy creating a small costing calculator for the signage department.
I'm not getting the calculator to output the amount.
Brief Description:
You enter the height and width and then when you hit enter it needs to display the cost.
How do I get it to work? Any suggestions please and thanks.
from tkinter import *
from tkinter import ttk
#Define the Functions here
def squeare(height,width):
cost = ((float(height) * float(width))/1000000 * 650 * 1.15 * 1.50)
return cost
window = Tk()
window.title("Costing Calculator V1.0")
mainframe = ttk.Frame(window, padding="20 20 20 20")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
window.columnconfigure(0, weight=1)
window.rowconfigure(0, weight=1)
height = StringVar()
width = StringVar()
#ttk.Label(mainframe, text="H").grid(column=1, row=1, sticky=E)
height_entry = ttk.Entry(mainframe, width=7, textvariable=height)
height_entry.grid(column=3, row=1, sticky=(W,E))
ttk.Label(mainframe, text="X").grid(column=5, row=1, sticky=E)
#ttk.Label(mainframe, text="W").grid(column=6, row=1, sticky=E)
width_entry = ttk.Entry(mainframe, width=7, textvariable=width)
width_entry.grid(column=7, row=1, sticky=(W,E))
ttk.Label(mainframe, text="=").grid(column=8, row=1, sticky=E)
#call function
#squeare()
window.mainloop()
I have bound the window frame with the return key. There is an empty result label. Whenever you press the enter key, its text will be updated.
from tkinter import *
from tkinter import ttk
def squeare(height, width):
cost = float(height) * float(width)/1000000 * 650 * 1.15 * 1.50
result.configure(text=str(cost))
window = Tk()
window.title("Costing Calculator V1.0")
mainframe = ttk.Frame(window, padding="20 20 20 20")
mainframe.grid(column=0, row=0, sticky="nsew")
window.columnconfigure(0, weight=1)
window.rowconfigure(0, weight=1)
height = StringVar()
height.set(0)
width = StringVar()
width.set(0)
height_entry = ttk.Entry(mainframe, width=7, textvariable=height)
height_entry.grid(column=0, row=0, sticky="we")
ttk.Label(mainframe, text="X").grid(column=1, row=0, sticky="e")
width_entry = ttk.Entry(mainframe, width=7, textvariable=width)
width_entry.grid(column=2, row=0, sticky=(W,E))
ttk.Label(mainframe, text="=").grid(column=3, row=0, sticky="e")
result = ttk.Label(mainframe)
result.grid(row=0, column=4)
window.bind('<Return>', lambda e: squeare(height.get(), width.get()))
window.mainloop()
The is to catch the 'enter' event for the second input field.
from tkinter import *
from tkinter import ttk
#Define the Functions here
def squeare(height,width):
cost = ((float(height) * float(width))/1000000 * 650 * 1.15 * 1.50)
result.configure(text=str(cost))
def enter(event=None):
squeare(height.get(), width.get())
window = Tk()
window.title("Costing Calculator V1.0")
mainframe = ttk.Frame(window, padding="20 20 20 20")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
window.columnconfigure(0, weight=1)
window.rowconfigure(0, weight=1)
height = StringVar()
width = StringVar()
#ttk.Label(mainframe, text="H").grid(column=1, row=1, sticky=E)
height_entry = ttk.Entry(mainframe, width=7, textvariable=height)
height_entry.grid(column=3, row=1, sticky=(W,E))
ttk.Label(mainframe, text="X").grid(column=5, row=1, sticky=E)
#ttk.Label(mainframe, text="W").grid(column=6, row=1, sticky=E)
width_entry = ttk.Entry(mainframe, width=7, textvariable=width)
width_entry.grid(column=7, row=1, sticky=(W,E))
width_entry.bind('<Return>',enter)
ttk.Label(mainframe, text="=").grid(column=8, row=1, sticky=E)
result = ttk.Label(mainframe, text="")
result.grid(column=9, row=1, sticky=E)
window.mainloop()

crashes when using grid with ttk widgets

I need a specific layout for the gui I'm making with Tkinter/ttk on python 2.7, that's why I want to use the grid positioner to master my ttk widgets' relative positions. But everytime I run my code, nothing appears.
ps: I used some widgets with grid(), and some others with pack()
I don't know where's the problem! Here's the code:
from Tkinter import *
import ttk
master = Tk()
f4=Frame(master,width=300,height=300,bg="powder blue",relief=SUNKEN)
f4.pack(side=BOTTOM,fill=BOTH, expand=True)
test = True
f3=Frame(master,width=300,height=300,bg="red",relief=SUNKEN)
f3.pack(side=TOP,fill=BOTH, expand=True)
def create():
global test
if test:
global e
e = ttk.Entry(f4).grid(row=2,column=0, columnspan=2)
test = False
#e.focus_set()
def callback():
print e.get()
b = Button(master, text="get", width=10, command=callback)
b.grid(row=0, column=0)
c = Button(master, text="set", width=10, command=callback)
c.grid(row=0, column=1)
create()
mainloop()
Your problem is the use of pack() and grid() at the same time on the master window.
Use one or the other per container.
A container is the main root window, a Toplevel() window or a Frame.
Try something like this without pack():
from Tkinter import *
import ttk
master = Tk()
master.rowconfigure(0, weight=1)
master.columnconfigure(0, weight=1)
master.columnconfigure(1, weight=1)
f3=Frame(master,width=300,height=300,bg="red",relief=SUNKEN)
f3.grid(row=0, column=0, columnspan=2, sticky="nsew")
f4=Frame(master, width=300, height=300, bg="powder blue", relief=SUNKEN)
f4.grid(row=1, column=0, columnspan=2, sticky="nsew")
test = True
def create():
global test
if test:
global e
e = ttk.Entry(f4).grid(row=2,column=0, columnspan=2)
test = False
def callback():
print e.get()
b = Button(master, text="get", width=10, command=callback)
b.grid(row=2, column=0, sticky="e")
c = Button(master, text="set", width=10, command=callback)
c.grid(row=2, column=1, sticky="w")
create()
master.mainloop()
Here is a version just using pack().
I changed it up a little to give a different layout that might be closer to what you are trying.
from Tkinter import *
import ttk
master = Tk()
f3 = Frame(master, width=300, height=300, bg="red", relief=SUNKEN)
f3.pack(side=TOP, fill=BOTH, expand=True)
f4 = Frame(master, width=300, height=300, bg="powder blue", relief=SUNKEN)
f4.pack(side=TOP, fill=BOTH)
f5 = Frame(master, width=300)
f5.pack(side=BOTTOM, fill=BOTH)
test = True
def create():
global test
if test:
global e
e = ttk.Entry(f4).pack(side=TOP,fill=BOTH)
test = False
def callback():
print e.get()
b = Button(f5, text="get", width=10, command=callback)
b.pack(side=LEFT, fill=BOTH, expand=True)
c = Button(f5, text="set", width=10, command=callback)
c.pack(side=RIGHT, fill=BOTH, expand=True)
create()
master.mainloop()

Clear contents of a frame when function is called

My program creates different widgets depending on the selection from a radio button. Everything works great except I can't seem to clear the old widget if the other radio button is selected. The suggestion here: (https://stackoverflow.com/a/15995920/3924118) isn't working. Here's the relevant code.
From the main program:
root = Tk()
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
secondframe = ttk.Frame(mainframe)
secondframe.grid(column=4, row=3)
secondframe.columnconfigure(0, weight=1)
secondframe.columnconfigure(0, weight=1)
And then the function:
def pct_from_duration():
""" Calculate needed pct from target duration"""
tgt_dur_entry = ttk.Entry(mainframe, width=4, textvariable=tgt_dur_inp)
tgt_dur_entry.grid(column=5, row=3, sticky=(W, E))
for widget in secondframe.winfo_children():
widget.destroy()
ttk.Label(secondframe, textvariable=pct_bond_end).grid(column=1, row=1)
ttk.Button(mainframe, text="Calculate", command=calculate).grid(column=5, row=4, sticky=W)
FWIW, I get no error message, it just doesn't actually delete the widgets. This is all python 3.6.
Maybe this will help...I'ts an example of your function and it is working(Python 3.5)
import tkinter as tk
from tkinter import *
from tkinter import ttk
class GUI:
def __init__(self, master):
def pct_from_duration():
tgt_dur_entry = ttk.Entry(mainframe, width=4)
tgt_dur_entry.grid(column=5, row=3, sticky=(W, E))
for widget in secondframe.winfo_children():
widget.destroy()
l3 = ttk.Label(secondframe, text = 'Label').grid(column=1, row=1)
b2 = ttk.Button(mainframe, text="Calculate").grid(column=5, row=4, sticky=W)
self.master = master
mainframe = ttk.Frame(master)
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
secondframe = ttk.Frame(mainframe)
secondframe.grid(column=4, row=3)
secondframe.columnconfigure(0, weight=1)
secondframe.columnconfigure(0, weight=1)
l = Label(secondframe, text = 'Child_mainframe')
l.grid()
l2 = Label(mainframe, text = 'Child_secondframe')
l2.grid()
r1 = Radiobutton(master, text="Radiobutton", value=1, command = pct_from_duration).grid()
root = Tk()
root.rowconfigure(0, weight = 1)
root.columnconfigure(1, weight=1)
root.columnconfigure(0, weight=1)
root.columnconfigure(0, weight=2)
my_gui = GUI(root)
root.mainloop()

Resizing window doesn't resize contents in tkinter

I'm making my first GUI application and I've run into a silly problem. Resizing the main window doesn't resize its contents and leaves blank space. I've read the TKDocs and they only say you should use sticky and column/row weight attributes but I don't really understand how they work.
Here's my code (only the part covering widgets, if you think problem isn't here I'll post the rest of it):
from tkinter import *
from tkinter import ttk
root = Tk()
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
player1 = StringVar()
player2 = StringVar()
player1.set('Player 1')
player2.set('Player 1')
timer=StringVar()
running=BooleanVar()
running.set(0)
settimer = ttk.Entry(mainframe, width=7, textvariable=timer)
settimer.grid(column=2, row=1, sticky=(N, S))
ttk.Button(mainframe, text="Start", command=start).grid(column=2, row=2, sticky=(N, S))
ttk.Label(mainframe, textvariable=player1, font=TimeFont).grid(column=1, row=3, sticky=(W, S))
ttk.Label(mainframe, textvariable=player2, font=TimeFont).grid(column=3, row=3, sticky=(E, S))
for child in mainframe.winfo_children():
child.grid_configure(padx=80, pady=10)
root.mainloop()
Thanks for your time!
Maybe this will help you in the right direction. Be sure to configure column/row weights at each level.
import tkinter.ttk
from tkinter.constants import *
class Application(tkinter.ttk.Frame):
#classmethod
def main(cls):
tkinter.NoDefaultRoot()
root = tkinter.Tk()
app = cls(root)
app.grid(sticky=NSEW)
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)
root.resizable(True, False)
root.mainloop()
def __init__(self, root):
super().__init__(root)
self.create_variables()
self.create_widgets()
self.grid_widgets()
self.grid_columnconfigure(0, weight=1)
def create_variables(self):
self.player1 = tkinter.StringVar(self, 'Player 1')
self.player2 = tkinter.StringVar(self, 'Player 2')
self.timer = tkinter.StringVar(self)
self.running = tkinter.BooleanVar(self)
def create_widgets(self):
self.set_timer = tkinter.ttk.Entry(self, textvariable=self.timer)
self.start = tkinter.ttk.Button(self, text='Start', command=self.start)
self.display1 = tkinter.ttk.Label(self, textvariable=self.player1)
self.display2 = tkinter.ttk.Label(self, textvariable=self.player2)
def grid_widgets(self):
options = dict(sticky=NSEW, padx=3, pady=4)
self.set_timer.grid(column=0, row=0, **options)
self.start.grid(column=0, row=1, **options)
self.display1.grid(column=0, row=2, **options)
self.display2.grid(column=0, row=3, **options)
def start(self):
timer = self.timer.get()
self.player1.set(timer)
self.player2.set(timer)
if __name__ == '__main__':
Application.main()

Categories

Resources