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()
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??
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()
I have three frames, but when a new label appears the frames automatically readjust to a new size. How do I stop the readjustment and have the size of each frame set and immutable.
#Import tkinter to make gui
from tkinter import *
from tkinter import ttk
import codecs
#Program that results when user attempts to log in
def login(*args):
file = open("rot13.txt", "r")
lines = file.readlines()
uname = user.get()
pword = pw.get()
for i in lines:
x = i.split()
if codecs.encode(uname,'rot13') == x[0] and codecs.encode(pword,'rot13') == x[1]:
result.set("Successful")
break;
else:
result.set("Access Denied")
root = Tk()
root.title("Login")
#Configures column and row settings and sets padding
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe['borderwidth'] = 5
mainframe['relief'] = "solid"
mainframe.grid(column=1, row=1, columnspan=3, rowspan=2)
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
mainframe2 = ttk.Frame(root, padding="3 3 12 12")
mainframe2['borderwidth'] = 5
mainframe2['relief'] = "solid"
mainframe2.grid(column=1, row=3, columnspan=1, rowspan=3)
mainframe2.columnconfigure(0, weight=1)
mainframe2.rowconfigure(0, weight=1)
mainframe3 = ttk.Frame(root, padding="3 3 12 12")
mainframe3['borderwidth'] = 5
mainframe3['relief'] = "solid"
mainframe3.grid(column=2, row=5)
mainframe3.columnconfigure(0, weight=1)
mainframe3.rowconfigure(0, weight=1)
#anchors for widgets
user = StringVar()
pw = StringVar()
result = StringVar()
#Asks user input
user_entry = ttk.Entry(mainframe, width=20, textvariable=user)
user_entry.grid(column=2, row=1, sticky=(W, E))
pw_entry = ttk.Entry(mainframe, width=20, textvariable=pw)
pw_entry.grid(column=2, row=2, sticky=(W, E))
#Labels to make user-friendly and able to understand
ttk.Label(mainframe, text="Username ").grid(column=1, row=1, sticky=W)
ttk.Label(mainframe, text="Password ").grid(column=1, row=2, sticky=W)
ttk.Label(mainframe2, text="").grid(column=1, row=3, sticky=W)
ttk.Label(mainframe2, text="Result").grid(column=1, row=4, sticky=W)
ttk.Label(mainframe2, text="").grid(column=1, row=5, sticky=W)
#Button to log in
ttk.Button(mainframe3, text="Login", command=login).grid(column=3, row=5, sticky=(W,E))
#Makes a spot to put in result
ttk.Label(mainframe2, textvariable=result).grid(column=2, row=4, sticky=(W, E))
#Opens up with item selected and allows you to enter username without having to click it
user_entry.focus()
#Runs calculate if click enter
root.bind('<Return>', login)
root.mainloop()
Here is the before and after picture of the results:
As you can see the frames change sizes after the program runs its course. How do i stop this re-size and stick to a predetermined size?
The simplest solution in this specific case is to give the label with the text a fixed width. When you specify a fixed width for a label, tkinter will do it's best to honor that width (though a lot depends on how you place it on the screen with pack, place or grid).
ttk.Label(..., width=12).grid(...)
Another solution is to turn off geometry propagation, which means you're responsible for giving the frame an explicit width and height:
mainframe2 = ttk.Frame(..., width=200, height=100)
...
mainframe2.grid_propagate(False)
I do not recommend turning geometry propagation off. Tkinter is usually very good at computing the right size for widgets, and this usually results in very poor behavior if you change fonts, screen resolutions, or root window sizes.
Just add a width option to ttk.Label(mainframe2, textvariable=result).grid(column=2, row=4, sticky=(W, E)):
So it will become like this:
ttk.Label(mainframe2, textvariable=result, width=20).grid(column=2, row=4, sticky=(W, E))
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 found this code online and i wanted to try it out because im trying to figure out how to have my label to change while i type things into my messagebox. I tried the getmethod but i have been struggling with using it. So i found this code and when i tried it i get the error that ttk is undefined but it clearly is.
from Tkinter import *
from ttk import *
def calculate(*args):
try:
value = float(feet.get())
meters.set((0.3048 * value * 10000.0 + 0.5)/10000.0)
except ValueError:
pass
root = Tk()
root.title("Feet to Meters")
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)
feet = StringVar()
meters = StringVar()
feet_entry = ttkEntry(mainframe, width=7, textvariable=feet)
feet_entry.grid(column=2, row=1, sticky=(W, E))
ttk.Label(mainframe, textvariable=meters).grid(column=2, row=2, sticky=(W, E))
ttk.Button(mainframe, text="Calculate", command=calculate).grid(column=3, row=3, sticky=W)
ttk.Label(mainframe, text="feet").grid(column=3, row=1, sticky=W)
ttk.Label(mainframe, text="is equivalent to").grid(column=1, row=2, sticky=E)
ttk.Label(mainframe, text="meters").grid(column=3, row=2, sticky=W)
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
feet_entry.focus()
root.bind('<Return>', calculate)
root.mainloop()
Traceback (most recent call last):
File "tk8.py", line 15, in
mainframe = ttk.Frame(root, padding="3 3 12 12")
NameError: name 'ttk' is not defined
So i found this code and when i tried it i get the error that ttk is
undefined but it clearly is.
You're star-importing from the module, though, using from ttk import *, so the name ttk doesn't refer to anything. For example, from math import * would bring sin, cos, etc., all into your namespace but the name math would still be undefined. The code works for me if I switch the imports to
from Tkinter import *
import ttk
and add the missing . from ttk.Entry in this line:
feet_entry = ttk.Entry(mainframe, width=7, textvariable=feet)
You are looking for the trace_variable method. Here is a fixed version:
from Tkinter import Tk, StringVar
import ttk
def calculate(*args):
try:
value = float(feet.get())
meters.set('%g' % (0.3048 * value))
except ValueError:
if not feet.get():
meters.set('')
root = Tk()
root.title("Feet to Meters")
feet = StringVar()
feet.trace_variable('w', calculate)
meters = StringVar()
main = ttk.Frame(root)
main.grid(sticky='nsew')
ttk.Label(main, text="Feet:").grid(row=0, sticky='e')
feet_entry = ttk.Entry(main, width=7, textvariable=feet)
feet_entry.grid(row=0, column=1, sticky='ew')
feet_entry.focus()
ttk.Label(main, text="Meters:").grid(row=1, sticky='e')
ttk.Label(main, textvariable=meters).grid(row=1, column=1, sticky='ew')
root.mainloop()