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()
Related
root frame weight is correct when button btnOpen is not present (commented). Otherwise the weight of root and frLeft is about 1:1. Why button (or frame frButtons) makes a difference?
Here is the code:
from tkinter import *
root = Tk()
root.title("xxx")
root.geometry("500x400")
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=15)
root.columnconfigure(1, weight=10)
frLeft = Frame(root, bg="#808080")
frLeft.grid(row=0, column=0, sticky="NSEW")
frRight = Frame(root, bg="#FAF0F0")
frRight.grid(row=0, column=1, sticky="NSEW")
frRight.rowconfigure(0, weight=1)
frButtons = Frame(frRight, bg="red")
frButtons.grid(row=0, column=0, sticky="W", padx=10, pady=10)
btnOpen = Button(frButtons, command=open, text='Open', padx=2).grid(row=0, sticky="WS", padx=10)
root.mainloop()
Correct:
Not correct:
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()
When you enter a file name in the entry filed and click the "Open" button, the content will be displayed.
I want the Entry and Text widget to completely fill Frame3 (red) and continue to do so when the application window is resized. How do I achieve this?
This is my code:
from Tkinter import *
ALL = N+S+W+E
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master.rowconfigure(0, weight=1)
self.master.columnconfigure(0, weight=1)
self.grid(sticky=ALL)
def handler(event):
print("clicked at", event.x, event.y)
def show_entry_fields():
#print e1.get()
f=open(e1.get())
out_put=f.read()
l1=Label(f3, text=out_put,fg="purple").grid(row=5,column=2)
return out_put
def call_red():
out_put=show_entry_fields()
Label(f3, text=out_put,fg="red").grid(row=5,column=2)
def call_green():
out_put=show_entry_fields()
Label(f3, text=out_put,fg="green").grid(row=5,column=2)
def call_blue():
out_put=show_entry_fields()
Label(f3, text=out_put,fg="blue").grid(row=5,column=2)
def call_black():
out_put=show_entry_fields()
Label(f3, text=out_put,fg="black").grid(row=5,column=2)
for r in range(4):
self.rowconfigure(r, weight=1)
self.master.rowconfigure(0, weight=1)
self.columnconfigure(0, weight=1)
b1=Button(self, text="Red",command=call_red).grid(row=5, column=0, sticky=ALL)
self.columnconfigure(1, weight=1)
b2=Button(self, text="Blue",command=call_blue).grid(row=5, column=1, sticky=ALL)
self.columnconfigure(2, weight=1)
b3=Button(self, text="Green",command=call_green).grid(row=5, column=2, sticky=ALL)
self.columnconfigure(3, weight=1)
b4=Button(self, text="Black",command=call_black ).grid(row=5, column=3, sticky=ALL)
self.columnconfigure(4, weight=1)
b5=Button(self, text="Open",command=show_entry_fields).grid(row=5, column=4, sticky=ALL)
#------------------------------
f1 = Frame(self, bg="blue")
f1.grid(row=0, column=0, rowspan=2,columnspan=2, sticky=ALL)
f1.bind("<Button-1>", handler)
f1.focus()
f2 = Frame(self, bg="green")
f2.grid(row=2, column=0, rowspan=2,columnspan=2, sticky=ALL)
f2.bind("<Button-1>", handler)
f2.focus()
f3 = Frame(self, bg="red")
f3.grid(row=0, column=2, rowspan=4, columnspan=4, sticky=ALL)
l=Label(f3, text="Enter File Path:").grid(row=1)
e1 = Entry(f3)
e1.grid(row=1,column=2)
root = Tk()
app = Application(master=root)
app.mainloop()
You should assign a weight to the column and row in which you place the text:
f3.grid_columnconfigure(2, weight=1)
f3.grid_rowconfigure(5, weight=1)
This tells Tkinter that it should distribute any extra space to that row and column, which will make the cell grow upon resize.
You might want to add sticky=ALL to your e1 then too, so it resizes with the text below it.
I am working on a Tkinter doesn't jump out problem.
There is what I am running:
from Tkinter import *
import ttk
def plus(*args):
value = float(a.get())
value1 = float(b.get())
result.set(value + value1)
print "the result is " + str(result.get())
root = Tk()
root.title("Plus them")
mainframe = ttk.Frame(root, padding="10 10 10 10")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
a = StringVar()
b = StringVar()
result = StringVar()
feet_entry = ttk.Entry(mainframe, width=5, textvariable=a)
feet_entry.grid(column=2, row=1, sticky=(W, E))
feet_entry1 = ttk.Entry(mainframe, width=5, textvariable=b)
feet_entry1.grid(column=5, row=1, sticky=(W, E))
ttk.Label(mainframe, text="the result is").grid(column=3, row=2, sticky=W)
ttk.Label(mainframe, textvariable = result).grid(column=5, row=2, sticky=(W, E))
ttk.Button(mainframe, text="Plus", command=plus).grid(column=3, row=3, sticky=W)
for child in mainframe.winfo_children():
child.grid_configure(padx=5, pady=5)
feet_entry.focus()
root.bind('<Return>', plus)
root.mainloop()
When it runs, it seems fine. But it doesn’t “jump out” no matter how many times I click the “Plus”, until I input new values for calculation and it still waiting for new entry.
How can I adjust to have it calculate only for once? Thanks.
To make your window execute only once and have plus button closing the window do as follow:
from Tkinter import *
import ttk
def plus(*args):
value = float(a.get())
value1 = float(b.get())
result.set(value + value1)
print "the result is " + str(result.get())
root.destroy() ####### look here !!!#######
root = Tk()
root.title("Plus them")
mainframe = ttk.Frame(root, padding="10 10 10 10")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
a = StringVar()
b = StringVar()
result = StringVar()
feet_entry = ttk.Entry(mainframe, width=5, textvariable=a)
feet_entry.grid(column=2, row=1, sticky=(W, E))
feet_entry1 = ttk.Entry(mainframe, width=5, textvariable=b)
feet_entry1.grid(column=5, row=1, sticky=(W, E))
ttk.Label(mainframe, text="the result is").grid(column=3, row=2, sticky=W)
ttk.Label(mainframe, textvariable = result).grid(column=5, row=2, sticky=(W, E))
ttk.Button(mainframe, text="Plus", command=plus).grid(column=3, row=3, sticky=W)
# for child in mainframe.winfo_children():
# child.grid_configure(padx=5, pady=5)
feet_entry.focus()
root.bind('<Return>', plus)
root.mainloop()
So here, in your plus callbeck you need to call root.destroy(). Also this loop for child in mainframe.winfo_children() does not make sense in my option, and its not needed. So I removed it in the example.
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()