Ive been trying for a while now but i cant seem to display the contents of a dictionary in a label. I want to hit the display button and when i do, i want all the contents in my dictionary to display, see the code below:
import sys
from tkinter import *
from tkinter import ttk
import time
from datetime import datetime
now= datetime.now()
x = []
d = dict()
def quit():
print("Have a great day! Goodbye :)")
sys.exit(0)
def display():
for key in d.keys():
x.append(key)
def add(*args):
global stock
global d
stock = stock_Entry.get()
Quantity = Quantity_Entry.get()
if stock not in d:
d[stock] = Quantity
else:
d[stock] += Quantity
root = Tk()
root.title("Homework 5 216020088")
mainframe = ttk.Frame(root, padding="6 6 20 20")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
ttk.Label(mainframe, text="you are accesing this on day %s of month %s of %s" % (now.day,now.month,now.year)+" at exactly %s:%s:%s" % (now.hour,now.minute,now.second), foreground="yellow", background="Black").grid(column=0, row = 0)
stock_Entry= ttk.Entry(mainframe, width = 60, textvariable="stock")
stock_Entry.grid(column=0, row = 1, sticky=W)
ttk.Label(mainframe, text="Please enter the stock name").grid(column=1, row = 1, sticky=(N, W, E, S))
Quantity_Entry= ttk.Entry(mainframe, width = 60, textvariable="Quantity")
Quantity_Entry.grid(column=0, row = 2, sticky=W)
ttk.Label(mainframe, text="Please enter the quantity").grid(column=1, row = 2, sticky=(N, W, E, S))
ttk.Button(mainframe, text="Add", command=add).grid(column=0, row=3, sticky=W)
ttk.Button(mainframe, text="Display", command=display).grid(column=0, row=3, sticky=S)
ttk.Button(mainframe, text="Exit", command=quit).grid(column=0, row=3, sticky=E)
ttk.Label(mainframe, text= x).grid(column=0, row= 4, sticky=W)
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
root.mainloop()
Label(..., text= x) will display only once - at the start.
You have to use StringVar to do what you expect
x_var = StringVar()
Label(..., texvariable=x_var)
Now you can set value in x_var to automatically set new text in Label
x_var.set( x )
BTW: instead of
for key in d.keys():
x.append(key)
probably you should do
x = list(d.keys())
or maybe even
x = list(d)
EDIT: display all keys (as in your code) when you press button "Display"
import sys
from Tkinter import *
import ttk
import time
from datetime import datetime
# --- functions ----
def quit():
print("Have a great day! Goodbye :)")
sys.exit(0)
def display():
x_var.set( list(d) )
def add(*args):
global stock
global d
stock = stock_Entry.get()
Quantity = Quantity_Entry.get()
if stock not in d:
d[stock] = Quantity
else:
d[stock] += Quantity
# --- main ---
x = list()
d = dict()
now = datetime.now()
root = Tk()
root.title("Homework 5 216020088")
x_var = StringVar()
mainframe = ttk.Frame(root, padding="6 6 20 20")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
ttk.Label(mainframe, text="you are accesing this on day %s of month %s of %s" % (now.day,now.month,now.year)+" at exactly %s:%s:%s" % (now.hour,now.minute,now.second), foreground="yellow", background="Black").grid(column=0, row = 0)
stock_Entry= ttk.Entry(mainframe, width = 60, textvariable="stock")
stock_Entry.grid(column=0, row = 1, sticky=W)
ttk.Label(mainframe, text="Please enter the stock name").grid(column=1, row = 1, sticky=(N, W, E, S))
Quantity_Entry= ttk.Entry(mainframe, width = 60, textvariable="Quantity")
Quantity_Entry.grid(column=0, row = 2, sticky=W)
ttk.Label(mainframe, text="Please enter the quantity").grid(column=1, row = 2, sticky=(N, W, E, S))
ttk.Button(mainframe, text="Add", command=add).grid(column=0, row=3, sticky=W)
ttk.Button(mainframe, text="Display", command=display).grid(column=0, row=3, sticky=S)
ttk.Button(mainframe, text="Exit", command=quit).grid(column=0, row=3, sticky=E)
ttk.Label(mainframe, textvariable=x_var).grid(column=0, row= 4, sticky=W)
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
root.mainloop()
from Tkinter import *
from Tkinter import ttk
import time
from datetime import datetime
# --- functions ----
def quits():
print("Have a great day! Goodbye :)")
time.sleep(3)
exit()
def display():
x_var.set(list(d))
def add():
global d
stock = stock_Entry.get()
quantity = Quantity_Entry.get()
if stock not in d:
d[stock] = quantity
else:
d[stock] += quantity
# --- main ---
x = list()
d = dict()
now = datetime.now()
root = Tk()
root.title("Homework 5 216020088")
x_var = StringVar()
mainframe = ttk.Frame(root, padding="6 6 20 20")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
ttk.Label(mainframe, text="you are accessing this on day %s of month %s of %s" % (
now.day, now.month, now.year) + " at exactly %s:%s:%s" % (now.hour, now.minute, now.second), foreground="yellow", background="Black").grid(column=0, row=0)
stock_Entry = ttk.Entry(mainframe, width=60, textvariable="stock")
stock_Entry.grid(column=0, row=1, sticky=W)
ttk.Label(mainframe, text="Please enter the stock name").grid(column=1, row=1,
sticky=(N, W, E, S))
Quantity_Entry = ttk.Entry(mainframe, width=60, textvariable="Quantity")
Quantity_Entry.grid(column=0, row=2, sticky=W)
ttk.Label(mainframe, text="Please enter the quantity").grid(column=1, row=2, sticky=
(N, W, E, S))
ttk.Button(mainframe, text="Add", command=add).grid(column=0, row=3, sticky=W)
ttk.Button(mainframe, text="Display", command=display).grid(column=0, row=3,
sticky=S)
ttk.Button(mainframe, text="Exit", command=quits).grid(column=0, row=3, sticky=E)
ttk.Label(mainframe, textvariable=x_var).grid(column=0, row=4, sticky=W)
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
root.mainloop()
Related
Im trying to get the path as a variable using Tkinter but filedialog.askdirectory not working in Mac i get this error on finder
root = Tk()
root.title("title")
def select_dir():
filepath=filedialog.askdirectory(initialdir="/Users/userx/Documents")
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
v1 = StringVar()
v1_entry = ttk.Entry(mainframe, width=12, textvariable=rpu)
v1_entry.grid(column=2, row=1, sticky=(W, E))
v2 = StringVar()
v2_entry = ttk.Entry(mainframe, width=25, textvariable=razon_soc)
v2_entry.grid(column=2, row=2, sticky=(W, E))
ttk.Button(mainframe, text="Select folder", command=select_dir).grid(column=1, row=3, sticky=W)
ttk.Label(mainframe, text="name_v1").grid(column=1, row=1, sticky=E)
ttk.Label(mainframe, text="name_v2").grid(column=1, row=2, sticky=E)
for child in mainframe.winfo_children():
child.grid_configure(padx=5, pady=5)
rpu_entry.focus()
root.bind("<Return>", select_dir)
root.mainloop()
also Im getting this warning
2022-09-10 00:57:28.395 python[1569:11380] Warning: Expected min height of view: (<NSButton: 0x7fb09add8d70>) to be less than or equal to 30 but got a height of 32.000000. This error will be logged once per view in violation.
Do you have any Idea why this is happening and how it can be solved??
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()
I am written a simple Tkinter program and i am trying to display both my stock name and stock Quantity, but i find that only my stock name displays. My code is as follows:
import sys
from tkinter import *
from tkinter import ttk
import time
from datetime import datetime
now= datetime.now()
x = []
d = dict()
def quit():
print("Have a great day! Goodbye :)")
sys.exit(0)
def display():
x_var.set(list(d))
def add(*args):
global stock
global d
global Quantity
stock = stock_Entry.get()
Quantity = int(Quantity_Entry.get())
if stock not in d:
d[stock] = Quantity
else:
d[stock] += Quantity
root = Tk()
root.title("Homework 5 216020088")
x_var = StringVar()
x_var.set(x)
mainframe = ttk.Frame(root, padding="6 6 20 20")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
ttk.Label(mainframe, text="you are accesing this on day %s of month %s of %s" % (now.day,now.month,now.year)+" at exactly %s:%s:%s" % (now.hour,now.minute,now.second), foreground="yellow", background="Black").grid(column=0, row = 0)
stock_Entry= ttk.Entry(mainframe, width = 60, textvariable="stock")
stock_Entry.grid(column=0, row = 1, sticky=W)
ttk.Label(mainframe, text="Please enter the stock name").grid(column=1, row = 1, sticky=(N, W, E, S))
Quantity_Entry= ttk.Entry(mainframe, width = 60, textvariable="Quantity")
Quantity_Entry.grid(column=0, row = 2, sticky=W)
ttk.Label(mainframe, text="Please enter the quantity").grid(column=1, row = 2, sticky=(N, W, E, S))
ttk.Button(mainframe, text="Add", command=add).grid(column=0, row=3, sticky=W)
ttk.Button(mainframe, text="Display", command=display).grid(column=0, row=3, sticky=S)
ttk.Button(mainframe, text="Exit", command=quit).grid(column=0, row=3, sticky=E)
ttk.Label(mainframe, textvariable= x_var).grid(column=0, row= 4, sticky=W)
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
root.mainloop()
Now, this is only displaying my stock and not the Quantity, can you please help me or recommend what I should do?
Currently, in your display() function you are only changing x_var which refers to the stock.
Have you tried adding another tk.Label and bind a quantity variable to it?
You can try this:
import sys
from tkinter import *
from tkinter import ttk
import time
from datetime import datetime
now = datetime.now()
x = []
d = {}
def quit():
print("Have a great day! Goodbye :)")
sys.exit(0)
def display():
x = []
for key, value in d.items():
x.append([key, value])
print(x)
x_var.set(x)
def add(*args):
global stock
global d
global Quantity
stock = stock_Entry.get()
Quantity = int(Quantity_Entry.get())
if stock not in d:
d[stock] = Quantity
else:
d[stock] += Quantity
root = Tk()
root.title("Homework 5 216020088")
x_var = StringVar()
x_var.set(x)
mainframe = ttk.Frame(root, padding="6 6 20 20")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
ttk.Label(mainframe, text="you are accesing this on day %s of month %s of %s" % (
now.day, now.month, now.year) + " at exactly %s:%s:%s" % (now.hour, now.minute, now.second), foreground="yellow",
background="Black").grid(column=0, row=0)
stock_Entry = ttk.Entry(mainframe, width=60, textvariable="stock")
stock_Entry.grid(column=0, row=1, sticky=W)
ttk.Label(mainframe, text="Please enter the stock name").grid(column=1, row=1, sticky=(N, W, E, S))
Quantity_Entry = ttk.Entry(mainframe, width=60, textvariable="Quantity")
Quantity_Entry.grid(column=0, row=2, sticky=W)
ttk.Label(mainframe, text="Please enter the quantity").grid(column=1, row=2, sticky=(N, W, E, S))
ttk.Button(mainframe, text="Add", command=add).grid(column=0, row=3, sticky=W)
ttk.Button(mainframe, text="Display", command=display).grid(column=0, row=3, sticky=S)
ttk.Button(mainframe, text="Exit", command=quit).grid(column=0, row=3, sticky=E)
ttk.Label(mainframe, textvariable=x_var).grid(column=0, row=4, sticky=W)
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
root.mainloop()
I am trying to create a series of two pop of windows where the first will determine the number of coordinates the user will input and the second will allow the user to input this exact number of coordinates.
My code for the first pop up works and looks like this:
root = Tk()
root.title("User Inputs")
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)
numofcoord = StringVar()
numofcoord = ttk.Entry(mainframe, width=7, textvariable=numofcoord)
numofcoord.grid(column=2, row=1, sticky=(W, E))
ttk.Button(mainframe, text="OK", command=user_coordinates).grid(column=1, row=2, sticky=W)
ttk.Button(mainframe, text="CANCEL", command=quit).grid(column=2, row=2, sticky=W)
ttk.Label(mainframe, text="Number of Coordinates").grid(column=1, row=1, sticky=W)
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
root.mainloop()
The first section works, but in the second section, my pop up looks like this where the number of rows is determined by the input in the first pop up window.
http://i.imgur.com/dToI2qX.png
the code for the second section looks like this:
try:
noc = int(numofcoord.get())
root = Tk()
root.title("User Defined Coordinates")
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)
x = StringVar()
y = StringVar()
ttk.Label(mainframe, text="X").grid(column=1, row=1, sticky=(W, E))
ttk.Label(mainframe, text="Y").grid(column=2, row=1, sticky=(W, E))
for i in range(2, noc + 2):
xcoord = ttk.Entry(mainframe, width=7, textvariable=x)
xcoord.grid(column=1, row=i, sticky=(W, E))
ycoord = ttk.Entry(mainframe, width=7, textvariable=y)
ycoord.grid(column=2, row=i, sticky=(W, E))
ttk.Button(mainframe, text="OK", command=test).grid(column=1, row=noc + 3, sticky=W)
ttk.Button(mainframe, text="CANCEL", command=quit).grid(column=2, row=noc + 3, sticky=W)
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
root.mainloop()
except ValueError:
pass
The issue is that in when you put a value in either the X or Y column, every box in that column is filled with that value and I would like for each row to be a set of coordinates.
The problem is that all of your entry widgets share the same StringVar. You need to create a new instance of StringVar for each entry widget. Though, you don't really need to use StringVar at all. All you really need to do is keep a reference to each entry widget.
For example:
x_entries = []
y_entries = []
for i in range(2, noc + 2):
xcoord = ttk.Entry(mainframe, width=7)
xcoord.grid(column=1, row=i, sticky=(W, E))
ycoord = ttk.Entry(mainframe, width=7)
ycoord.grid(column=2, row=i, sticky=(W, E))
x_entries.append(xcoord)
y_entries.append(ycoord)
Later, you can get the values like this:
print("first pair: ", x_entries[0].get(), y_entries[0].get())
You can do the same thing by using StringVar, but in most cases that adds complexity by giving you more objects to manage but without giving any real benefit.
Here's an example:
x_vars = []
y_vars = []
for i in range(2, noc + 2):
x = StringVar()
y = StringVar()
xcoord = ttk.Entry(mainframe, width=7, textvariable=x)
xcoord.grid(column=1, row=i, sticky=(W, E))
ycoord = ttk.Entry(mainframe, width=7, textvariable=y)
ycoord.grid(column=2, row=i, sticky=(W, E))
x_vars.append(x)
y_vars.append(y)
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.