I am getting name error while using tkinter - python

I am making an app that would calculate area, tsa, lsa, volume, etc. for various shapes
but I am getting this error:
NameError: name 'Square_Area' is not defined
but the function is exactly named same as the one in the line
I am so confused
this is the bit of code that is getting error
# layout for area of square
tsaf = Frame(root)
saf = Frame(root)
Label(tsaf, bg="gray", fg="blue", text="Square Area Calculator",
font=("calibri", 18, "italic")).pack(fill="x", ipady=10)
Label(saf, fg="red", text="Side of Square:",
font=("calibri", 18, "italic")).grid(row=0, column=0, pady=30, padx=30)
Label(saf, fg="red", text="Area of Square:",
font=("calibri", 18, "italic")).grid(row=1, column=0, pady=30, padx=30)
Entry(saf, fg="blue", borderwidth=6, textvariable=SquareSide,
font=("comicsansms", 16, "bold")).grid(row=0, column=1, pady=30, padx=20, ipadx=50)
Entry(saf, fg="blue", borderwidth=6, textvariable=SquareArea,
font=("comicsansms", 16, "bold")).grid(row=1, column=1, pady=30, padx=20, ipadx=50)
Button(saf, fg="blue", text="Get Area", activeforeground="red", borderwidth=4, command =
Square_Area,
font=("comicsansms", 16, "italic")).grid(row=2, column=1, pady=30, padx=50, ipadx=50)
and this is the function the button is using
def Square_Area():
side = float(SquareSide.get())
area = side*side
SquareArea.set(f"{area}")
I have tried everything on my side if anyone can pls help
if you need the full code I am listing it below
i will remove the unnecessary part
from tkinter import *
root = Tk()
# setting up window
root.geometry("600x400")
root.minsize(600, 400)
root.maxsize(600, 400)
root.title("Shape Calculator")
# declaring variable
SquareSide = StringVar()
SquareArea = StringVar()
# main page text
mt = Frame(root)
mt.pack()
title = Label(mt, fg="red", text="Select any field that you want to calculate",
font=("arial", 18, "bold"))
title.pack(padx=62, pady=185, side=LEFT)
# layout for area of square
tsaf = Frame(root)
saf = Frame(root)
Label(tsaf, bg="gray", fg="blue", text="Square Area Calculator",
font=("calibri", 18, "italic")).pack(fill="x", ipady=10)
Label(saf, fg="red", text="Side of Square:",
font=("calibri", 18, "italic")).grid(row=0, column=0, pady=30, padx=30)
Label(saf, fg="red", text="Area of Square:",
font=("calibri", 18, "italic")).grid(row=1, column=0, pady=30, padx=30)
Entry(saf, fg="blue", borderwidth=6, textvariable=SquareSide,
font=("comicsansms", 16, "bold")).grid(row=0, column=1, pady=30, padx=20, ipadx=50)
Entry(saf, fg="blue", borderwidth=6, textvariable=SquareArea,
font=("comicsansms", 16, "bold")).grid(row=1, column=1, pady=30, padx=20, ipadx=50)
Button(saf, fg="blue", text="Get Area", activeforeground="red", borderwidth=4, command =
Square_Area,
font=("comicsansms", 16, "italic")).grid(row=2, column=1, pady=30, padx=50, ipadx=50)
# menu functions
# area functions
def square_area():
kill_frame()
tsaf.pack(fill="both")
saf.pack(side=LEFT, anchor="nw")
# function to clear screen
def kill_frame():
mt.pack_forget()
saf.pack_forget()
tsaf.pack_forget()
# getting results
# area result functions
def Square_Area():
side = float(SquareSide.get())
area = side*side
SquareArea.set(f"{area}")
# making the menu
main_menu = Menu(root)
# 2d shape area
m1 = Menu(main_menu, tearoff=0)
m1.add_command(label="Square", command = square_area)
main_menu.add_cascade(label="Area calculator", menu=m1)
root.config(menu=main_menu)
root.mainloop()
i have removed everything about other shapes and just left the square part
sorry if its a bit messy

You need to define the function before you reference it. Since you are not using classes, you are doing procedural programming where python executes from top to bottom. You are referencing the function which is defined later, thus giving an error.
Define the function square_area anywhere before you reference it.
So you can do this:
from tkinter import *
root = Tk()
# setting up window
root.geometry("600x400")
root.minsize(600, 400)
root.maxsize(600, 400)
root.title("Shape Calculator")
# declaring variable
SquareSide = StringVar()
SquareArea = StringVar()
def square_area():
kill_frame()
tsaf.pack(fill="both")
saf.pack(side=LEFT, anchor="nw")
def Square_Area():
side = float(SquareSide.get())
area = side*side
SquareArea.set(f"{area}")
# main page text
mt = Frame(root)
mt.pack()
title = Label(mt, fg="red", text="Select any field that you want to calculate",
font=("arial", 18, "bold"))
title.pack(padx=62, pady=185, side=LEFT)
# layout for area of square
tsaf = Frame(root)
saf = Frame(root)
Label(tsaf, bg="gray", fg="blue", text="Square Area Calculator",
font=("calibri", 18, "italic")).pack(fill="x", ipady=10)
Label(saf, fg="red", text="Side of Square:",
font=("calibri", 18, "italic")).grid(row=0, column=0, pady=30, padx=30)
Label(saf, fg="red", text="Area of Square:",
font=("calibri", 18, "italic")).grid(row=1, column=0, pady=30, padx=30)
Entry(saf, fg="blue", borderwidth=6, textvariable=SquareSide,
font=("comicsansms", 16, "bold")).grid(row=0, column=1, pady=30, padx=20, ipadx=50)
Entry(saf, fg="blue", borderwidth=6, textvariable=SquareArea,
font=("comicsansms", 16, "bold")).grid(row=1, column=1, pady=30, padx=20, ipadx=50)
Button(saf, fg="blue", text="Get Area", activeforeground="red", borderwidth=4, command =
square_area,
font=("comicsansms", 16, "italic")).grid(row=2, column=1, pady=30, padx=50, ipadx=50)
# menu functions
# area functions
# function to clear screen
def kill_frame():
mt.pack_forget()
saf.pack_forget()
tsaf.pack_forget()
# getting results
# area result functions
# making the menu
main_menu = Menu(root)
# 2d shape area
m1 = Menu(main_menu, tearoff=0)
m1.add_command(label="Square", command = square_area)
main_menu.add_cascade(label="Area calculator", menu=m1)
root.config(menu=main_menu)
root.mainloop()

Related

Python TKinter Mac: Buttons layout strangely & click don't work

Python TKinter Mac: Buttons layout strangely and when a button is clicked it looks the same (as if it hadn't been clicked) however I didn't check to see if the button worked.
and the space is a textbook
the Mac version is: a 2017 iMac running Monterey 12.4
import tkinter as tk
window = tk.Tk()
window.geometry("500x500")
window.title("Stock Watchlist & Logger")
label = tk.Label(window, text="Tester", font=('Ariel', 18))
label.pack(padx=20, pady=20)
textbox = tk.Text(window, height=3, font=('Ariel', 16))
textbox.pack(padx=10, pady=10)
numbuttframe = tk.Frame(window)
numbuttframe.columnconfigure(0, weight=1)
yesnobuttframe = tk.Frame(window)
yesnobuttframe.columnconfigure(1, weight=1)
button1 = tk.Button(numbuttframe, text="1", font=('Arial', 18))
button1.grid(row=0, column=0, sticky=tk.W+tk.E)
button2 = tk.Button(numbuttframe, text="2", font=('Arial', 18))
button2.grid(row=0, column=1, sticky=tk.W+tk.E)
button3 = tk.Button(numbuttframe, text="3", font=('Arial', 18))
button3.grid(row=0, column=2, sticky=tk.W+tk.E)
button4 = tk.Button(numbuttframe, text="4", font=('Arial', 18))
button4.grid(row=0, column=3, sticky=tk.W+tk.E)
button5 = tk.Button(numbuttframe, text="5", font=('Arial', 18))
button5.grid(row=0, column=4, sticky=tk.W+tk.E)
button6 = tk.Button(numbuttframe, text="6", font=('Arial', 18))
button6.grid(row=0, column=5, sticky=tk.W+tk.E)
yesbutton = tk.Button(yesnobuttframe, text="Yes", font=('Arial', 18))
yesbutton.grid(row=0, column=0, sticky=tk.W+tk.E)
nobutton = tk.Button(yesnobuttframe, text="No", font=('Arial', 18))
nobutton.grid(row=0, column=1, sticky=tk.W+tk.E)
numbuttframe.pack(fill='x')
yesnobuttframe.pack(fill='x')
if button1:
print("CELEBRATE!")
window.mainloop()
You set the weight to two cells weight=1 , so they expanded, since the rest have a default weight of 0. Since I don’t see your grid - the buttons are placed in frames one after the other, suggest using the pack method. And shortened your code a bit. The buttons won't work yet, because they don't have a command = call. Complex variable names in Python are usually written with an underscore.
import tkinter as tk
FONT = ('Ariel', 18)
window = tk.Tk()
window.geometry("500x500")
window.title("Stock Watchlist & Logger")
lbl = tk.Label(window, text="Tester", font=FONT)
lbl.pack(padx=20, pady=20)
text_box = tk.Text(window, height=3, font=('Ariel', 16))
text_box.pack(padx=10, pady=10)
num_btn = tk.Frame(window)
yes_no_btn = tk.Frame(window)
num_btn.pack(fill='x')
yes_no_btn.pack(fill='x')
buttons = []
for i in range(1, 7):
btn = tk.Button(num_btn, text=f"{i}", font=FONT)
btn.pack(side="left", fill='x', expand=True)
buttons.append(btn)
yes_btn = tk.Button(yes_no_btn, text="Yes", font=FONT)
yes_btn.pack(side="left", fill='x', expand=True)
no_btn = tk.Button(yes_no_btn, text="No", font=FONT)
no_btn.pack(side="left", fill='x', expand=True)
window.mainloop()

Why is my tkinter scrollbar not working as intended? i hv removed the command line for the scrollbar

no error shown, i just removed the scrollbar command previously added as it wasnt working. i hope someone will be able to advise how to do this as i had gone thru multiple tutorials even those advises in stack overflow. i need to be able to scroll thru the entire window, below is the full code as requested.
import tkinter as tk
from tkinter import *
import time;
import datetime
from tkinter import ttk
from PIL import ImageTk, Image
import os
root = Tk()
root.title("Fibroscan Report")
root.geometry("1000x1200")
root.iconbitmap('d:/python_file/python_gui/zaclogo.ico')
frame0 = Frame(root, width=200, height=100)
frame0.pack()
frame0.pack_propagate(0)
frame1 = Frame(frame0, width=200, height=110, padx= 5, pady=5, bd=10,relief=RIDGE)
frame1.grid(row=0, column=0)
frame2 = Frame (frame0, width=200, padx= 5, pady=5, bd=10, relief= RIDGE)
frame2.grid(row=0, column=1)
frame3 = Frame(root, width=400, height=100)
frame3.pack()
frame3.pack_propagate(0)
frame4 = Frame(frame3, width=600, height=50, padx= 5, pady=5, bd=10, relief=RIDGE)
frame4.grid(row=0, column=0)
frame4.grid_propagate(0)
frame = Frame (root, width=200, height=100, padx=5, pady=15, relief=RIDGE)
frame.pack()
frame.pack_propagate(0)
framephoto = Frame (frame)
framephoto.grid()
canvas = Canvas(framephoto, width = 800, height = 324)
canvas.grid()
img = ImageTk.PhotoImage(Image.open("metavirchart2.png"))
canvas.create_image(107, 0, anchor=NW, image=img)
canvas.image = img
frame5 = LabelFrame (root,text="Fibroscan® Score Guide",font=('arial', 10), padx= 5, pady=5, bd=10, relief= RIDGE)
frame5.pack()
frame5.pack_propagate(0)
frame6 = Frame (frame5, width=230, height=50, padx= 5, pady=5, bd=10, relief= RIDGE)
frame6.grid(row=1, column=0)
frame6.grid_propagate(0)
frame7 = Frame (frame5, width=230, height=115, padx= 5, pady=5, bd=10, relief= RIDGE)
frame7.grid(row=2, column=0)
frame7.grid_propagate(0)
frame8 = Frame (frame5, width=230, height=50, padx= 5, pady=5, bd=10, relief= RIDGE)
frame8.grid(row=3, column=0)
frame8.grid_propagate(0)
frame9 = Frame (frame5, width=230, height=100, padx= 5, pady=5, bd=10, relief= RIDGE)
frame9.grid(row=4, column=0)
frame9.grid_propagate(0)
frame10 = Frame (frame5, width=515, height=50, padx= 5, pady=5, bd=10, relief= RIDGE)
frame10.grid(row=1, column=1)
frame10.grid_propagate(0)
frame11 = Frame (frame5, width=515, height=115, padx= 5, pady=5, bd=10, relief= RIDGE)
frame11.grid(row=2, column=1)
frame11.grid_propagate(0)
frame12 = Frame (frame5, width=515, height=50, padx= 5, pady=5, bd=10, relief= RIDGE)
frame12.grid(row=3, column=1)
frame12.grid_propagate(0)
frame13 = Frame (frame5, width=515, height=100, padx= 5, pady=5, bd=10, relief= RIDGE)
frame13.grid(row=4, column=1)
frame13.grid_propagate(0)
frameguide = Frame (frame5)
frameguide.grid(row=0, column=0)
frameguide.grid_propagate(0)
frame14 = Frame (frame13)
frame14.grid(row=0, column=1)
frame14.grid_propagate(0)
identity = StringVar()
fullname = StringVar()
refdr = StringVar(root)
datescan = StringVar()
dob = StringVar()
operator = StringVar()
operator.set('Insert Operator Name')
kpa = IntVar()
metavir = StringVar()
idrtype = StringVar()
idrtxt = StringVar()
lblidentity = Label(frame1, text="Patient ID", font=('arial', 10, 'bold'))
lblidentity.grid(row=0, column=0, sticky=W)
txtidentity = Entry(frame1, width=24, textvariable=identity, font=('arial', 10))
txtidentity.grid(row=0, column=1)
lblfullname = Label(frame1, text="Patient Name", font=('arial', 10, 'bold'))
lblfullname.grid(row=1, column=0, sticky=W)
txtfullname = Entry(frame1, width=24, textvariable=fullname, font=('arial', 10))
txtfullname.grid(row=1, column=1)
lbldob = Label(frame1, text="Date of Birth", font=('arial', 10, 'bold'))
lbldob.grid(row=2, column=0, sticky=W)
txtdob = Entry(frame1, width=24,textvariable=dob, font=('arial', 10))
txtdob.grid(row=2, column=1)
lblrefdr = Label(frame1, text="Referring Doctor", font=('arial', 10, 'bold'))
lblrefdr.grid(row=3, column=0, sticky=W)
txtrefdr = ttk.Combobox(frame1, width=24,
values=[
"Operator1",
"Operator2",
"Operator3",
"Operator4",
"Operator5",
"Operator6",
"Operator7"], state = "readonly")
txtrefdr.grid(row=3, column=1)
txtrefdr.current(0)
lbldatescan = Label(frame2, text="Date of Scan", font=('arial', 10, 'bold'))
lbldatescan.grid(row=0, column=0, sticky=W)
txtdatescan = Entry(frame2, width=24, textvariable=datescan, font=('arial', 10))
txtdatescan.grid(row=0, column=1)
lblindi = Label(frame2, text="Indication", font=('arial', 10, 'bold'))
lblindi.grid(row=1, column=0, sticky=W)
txtindi = ttk.Combobox(frame2,width=24,
values=[
"Hepatitis B*",
"HCV-HIV co-infection*",
"Hepatitis C",
"Hepatitis C*",
"Chronic Cholestatic Diseases*",
"Alcohol**",
"Others",
"NAFLD***"], state="readonly")
txtindi.grid(row=1, column=1)
txtindi.current(0)
lblgender = Label(frame2, text="Gender", font=('arial', 10, 'bold'))
lblgender.grid(row=2, column=0, sticky=W)
gender = ["Male", "Female", "Unspecified"]
txtgender = ttk.Combobox(frame2, width=24,
values=gender, state="readonly")
txtgender.grid(row=2, column=1)
txtgender.current(0)
lblop = Label(frame2, text="Operator", font=('arial', 10, 'bold'))
lblop.grid(row=3, column=0, sticky=W)
txtop = Entry(frame2, width=24, textvariable=operator, font=('arial', 10))
txtop.grid(row=3, column=1)
lbltitle = Label(frame4, text="YOUR FIBROSCAN® SCORE (Median LSM) is: ")
lbltitle.grid(row=0, column=0)
lblkpa = Label(frame4, text="KPa ")
lblkpa.grid(row=0, column=2)
txtkpa = Entry(frame4, textvariable=kpa, font=('arial', 10), width=5)
txtkpa.grid(row=0, column=1)
lblmeta = Label(frame4, text=" Metavir Stage: ")
lblmeta.grid(row=0, column=3)
metavir_score = ["F0", "F0-F1", "F1", "F1-F2", "F2", "F2-F3", "F3-F4", "F4"]
txtmeta = ttk.Combobox(frame4, width=10,
values=metavir_score, textvariable=metavir, state="readonly")
txtmeta.grid(row=0, column=4)
txtmeta.current(0)
lbltitle2 = Label(frame6,
text="Underlying Liver Disease", font=('arial', 10, 'bold'))
lbltitle2.grid(sticky=E)
lbltitle3 = Label(frame7,
text="Assessment", font=('arial', 10, 'bold'))
lbltitle3.grid(sticky=W)
lbltitle4 = Label(frame8,
text="Clinical Interpretation", font=('arial', 10, 'bold'))
lbltitle4.grid(sticky=W)
lbltitle5 = Label(frame9,
text="Recommended Clinical Actions", font=('arial', 10, 'bold'))
lbltitle5.grid(sticky=W)
lbltitleudl = Label(frame10,
text="next to UDL", font=('arial', 10, 'bold'))
lbltitleudl.grid()
lbltitle3a = Label(frame11,
text="Fibroscan Median LSM =", font=('arial', 10, 'bold'))
lbltitle3a.grid(row=0, column=0, sticky =W)
txtitle3a = Entry(frame11, width=16)
txtitle3a.grid(row=0, column=1)
lbltitle3a = Label(frame11,
text="Metavir Stage =", font=('arial', 10, 'bold'))
lbltitle3a.grid(row=1, column=0, sticky=W)
txtmetasg = ttk.Combobox(frame11, width=13,
values=metavir_score, state="readonly")
txtmetasg.grid(row=1, column=1)
txtmetasg.current(0)
lbltitle3a = Label(frame11,
text="Probe Type =", font=('arial', 10, 'bold'))
lbltitle3a.grid(row=2, column=0, sticky=W)
txtmeta = ttk.Combobox(frame11, width=13,
values=[
"S",
"M",
"XL",
"Others"], state="readonly")
txtmeta.grid(row=2, column=1)
txtmeta.current(0)
lbltitle3a = Label(frame11,
text="IQR/Median =", font=('arial', 10, 'bold'))
lbltitle3a.grid(row=3, column=0, sticky=W)
txtitle3a = Entry(frame11, width=16, textvariable=idrtype)
txtitle3a.grid(row=3, column=1)
lbltitle3a = Label(frame12,
text="Significant Fibrosis =", font=('arial', 13, 'bold'))
lbltitle3a.grid(row=0, column= 0)
txtmeta = ttk.Combobox(frame12, width=10,
values=[
"Yes",
"No"], state="readonly")
txtmeta.grid(row=0, column=1)
txtmeta.current(0)
fcolumn = Frame(frame13)
fcolumn.grid()
fcolumn1 = Frame(frame13)
fcolumn1.grid(sticky=W)
txtrca = ttk.Combobox(fcolumn, width=77, height=200,
values=[
"Repeat Fibroscan in 1 year",
"Patients with significant liver fibrosis have an increased risk of complications secondary \n to liver disease and development of HCC and should be considered \n for treatment of underlying aetiology or risk factor modification.",
"In patients with advanced fibrosis or cirrhosis, regular screening \n and surveillance for HCC is also recommended.",
])
txtrca.grid(row=0, column=0)
txtrca.current(0)
txtrca.grid_propagate(0)
lbliqr = Label(fcolumn1,
text="M Probe IQR/MEDIAN =", font=('arial', 10, 'bold'))
lbliqr.grid(row=1, column=0, sticky =W)
txtiqr = Entry(fcolumn1, width=16, textvariable=idrtxt)
txtiqr.grid(row=1, column=1)
lblguide = Label(fcolumn1,
text="*AASLD, APASL, EASL Guidelines", font=('arial', 10, 'bold'))
lblguide.grid(row=2, column=0, sticky =W)
root.mainloop()
any help will be appreciated.
no hate comments pls if u cant help or dun wish to help.
Try this
from tkinter import *
scroll_bar = Scrollbar(root)
scroll_bar.pack(side=RIGHT,fill=Y)

Can anyone help me to find mistake in my python GUI code?

def square():
def calculate():
global area
global perimeter
side = entry_variable.get()
area = side * side
area_variable.set(area)
perimeter = 4 * side
perimeter_variable.set(perimeter)
root = Tk()
root.minsize(width=400, height=400)
root.maxsize(width=400, height=400)
root.config(bg="Black")
entry_variable = IntVar()
area_variable = IntVar()
perimeter_variable = IntVar()
label1 = Label(root, text="", font=('Arial', 15, 'bold'), fg="white", bg='Black')
label1.grid()
label0 = Label(root, text="Enter the Side's measurement", font=('Arial', 15, 'bold'), fg="white", bg='Black')
label0.grid(row=1)
label00 = Label(root, text="", font=('Arial', 15, 'bold'), fg="white", bg='Black')
label00.grid(row=3)
entry1 = Entry(root, font=('Arial', 15, 'bold'), justify=RIGHT, textvariable=entry_variable, fg='black')
entry1.grid(row=2)
label2 = Label(root, text="Perimeter of the Square", font=('Arial', 15, 'bold'), fg="white", bg='Black')
label2.grid(row=5)
label3 = Label(root, text="Area of the Square", font=('Arial', 15, 'bold'), fg="white", bg='Black')
label3.grid(row=7)
entry2 = Entry(root, font=('Arial', 15, 'bold'), fg='black', justify=RIGHT, textvariable=perimeter_variable,
state='disabled')
entry2.grid(row=6)
entry3 = Entry(root, font=('Arial', 15, 'bold'), fg='black', justify=RIGHT, textvariable=area_variable,
state='disabled')
entry3.grid(row=8)
btn = Button(root, font=('Arial', 15, 'bold'), text='Calculate', bd=10, command=calculate)
btn.grid(row=4)
I creating a basic GUI to calculate square area and method.
I am using it in another GUI ( it's working perfectly) , I am accessing it using a button, that's why I created it a function.
It don't give any error but it don't execute result also.
I am a bigger so I am not much familiar OOPS!
It's not necessary to define two nested functions. Define one function in this way:
def calculate():
side = entry_variable.get()
area = side * side
area_variable.set(area)
perimeter = 4 * side
perimeter_variable.set(perimeter)

Why does my text not orientate to the left?

I want my text in to be shifted to the left, but justify, side nor anchor do this. I am using tkinter and python 3.
queue_frame = tk.Frame(master_frame)
queue_frame.pack()
queue_hash = tk.Label(queue_frame, text='#', width=3, font=('Arial', 14, 'bold'))
queue_hash.pack(side=tk.LEFT)
queue_name = tk.Label(queue_frame, text='Name', width=15, justify=tk.LEFT, font=('Arial', 14, 'bold'))
queue_name.pack(side=tk.LEFT)
queue_question_asked = tk.Label(queue_frame, text='Question Asked', width=25, font=('Arial', 14, 'bold'), justify=tk.LEFT)
queue_question_asked.pack(side=tk.LEFT)
queue_time = tk.Label(queue_frame, text='Time', width=15, font=('Arial', 14, 'bold'), justify=tk.LEFT)
queue_time.pack(side=tk.LEFT)
Thank you very much.
Here is a reference to Label options http://effbot.org/tkinterbook/label.htm
Use anchor with the option 'w' and if you add relief = raised you can see the relative location of the text within the label.
import tkinter as tk
master_frame = tk.Tk()
queue_frame = tk.Frame(master_frame)
queue_frame.pack()
queue_hash = tk.Label(queue_frame,
text='#',
width=3,
relief='raised')
queue_hash.pack(side=tk.LEFT)
queue_name = tk.Label(queue_frame,
text='Name',
width=15,
anchor='w',
relief='raised')
queue_name.pack(side=tk.LEFT)
queue_question_asked = tk.Label(queue_frame,
text='Question Asked',
width=25,
relief='raised',
anchor='w')
queue_question_asked.pack(side=tk.LEFT)
queue_time = tk.Label(queue_frame,
text='Time',
width=15,
relief='raised',
anchor='w')
queue_time.pack(side=tk.LEFT)
master_frame.mainloop()

How to fix issue with tkinter checkboxes?

I'm trying to make a python(2.7) Cafe System. I created most of the functions just one function It is supposed to after I click the check button to open up the entry. Just when I click the check box button it won't open does anyone know why?
from Tkinter import *
import random
import time
import datetime
root = Tk()
root.geometry("1350x750+0+0")
root.title("Cafe Management System")
root.configure(background='black')
Tops = Frame(root, width=1350, height=100, bd=14, relief="raise")
Tops.pack(side=TOP)
f1 = Frame(root, width=900, height=650, bd=8, relief="raise")
f1.pack(side=LEFT)
f2 = Frame(root, width=440, height=650, bd=8, relief="raise")
f2.pack(side=RIGHT)
f1a = Frame(f1, width=900, height=320, bd=6, relief="raise")
f1a.pack(side=TOP)
f2a = Frame(f1, width=900, height=320, bd=6, relief = "raise")
f2a.pack(side=BOTTOM)
ft2 = Frame(f2, width= 440,height=450,bd=12,relief="raise")
ft2.pack(side=TOP)
fb2 = Frame(f2, width=440, height=250, bd=16, relief="raise")
fb2.pack(side=BOTTOM)
f1aa = Frame(f1a, width=400, height=330, bd=16, relief="raise")
f1aa.pack(side=LEFT)
f1ab = Frame(f1a, width=400, height=330, bd=16, relief="raise")
f1ab.pack(side=RIGHT)
f2aa = Frame(f2a, width=450, height=330, bd=14, relief="raise")
f2aa.pack(side=LEFT)
f2ab = Frame(f2a, width=450, height=330, bd=14, relief="raise")
f2ab.pack(side=RIGHT)
Tops.configure(background='black')
f1.configure(background='black')
f2.configure(background='black')
lblInfo = Label(Tops, font=('arial', 70, 'bold'), text="Cafe Management")
lblInfo.grid(row=0, column=0)
#===============================Functions============================
def qExit():
root.destroy()
def Reset():
PaidTax.set("")
SubTotal.set("")
TotalCost.set("")
CostofDrinks.set("")
CostofCakes.set("")
ServiceCharge.set("")
txtReciept.delete("1.0",END)
E_Latta.set("0")
E_Coffee_Cake.set("0")
#===============================Variables============================
var1=IntVar()
DateofOrder = StringVar()
Reciept_Ref = StringVar()
PaidTax = StringVar()
SubTotal = StringVar()
TotalCost = StringVar()
CostofCakes=StringVar()
CostofDrinks=StringVar()
ServiceCharge=StringVar()
E_Latta = StringVar()
E_Coffee_Cake=StringVar()
E_Coffee_Cake.set("0")
DateofOrder.set(time.strftime("%H:%M:%S"))
#=========================DRINKS======================
Latta = Checkbutton(f1ab, text="Latte \t", variable = var1, onvalue = 1, offvalue = 0,
font=('arial',18,'bold')).grid(row=0, sticky=W)
#=======================Enter Widget For Cakes=================
txtLatta = Entry(f1aa, font=('arial', 16, 'bold'), bd=8, width=6, justify='left', textvariable=E_Coffee_Cake, state=DISABLED)
txtLatta.grid(row=0, column=1)
#===========================================================Check b
def chkbutton_value():
if (var1.get() == 1):
txtLatta.configure(state=NORMAL)
elif var1.get()==0:
txtLatta.configure(state=DISABLED)
E_Latta.set("0")
#===========================================================Check btns
var1.set(0)
txtLatta.configure(state=DISABLED)
#=======================================Infomation====
lblReciept = Label(ft2, font=('arial', 12, 'bold'), text="Reciept", bd=2).grid(row=0,column=0,sticky=W,)
txtReciept = Text(fb2,font=('arial',11,'bold'), bd=8, width=59)
txtReciept.grid(row=1, column=0)
#========================================Items
lblCostofDrinks=Label(f2aa,font=('arial', 16, 'bold'), text="Cost of Drinks", bd=8)
lblCostofDrinks.grid(row=0, column=0, sticky=W)
txtCostofDrinks=Entry(f2aa, font=('arial', 16, 'bold'), bd=8,
insertwidth=2,justify='left', textvariable=CostofDrinks)
txtCostofDrinks.grid(row=0, column=1, sticky=W)
lblCostofCakes=Label(f2aa,font=('arial', 16, 'bold'), text="Cost of Cakes", bd=8)
lblCostofCakes.grid(row=1, column=0, sticky=W)
txtCostofCakes=Entry(f2aa, font=('arial', 16, 'bold'), bd=8,
insertwidth=2,justify='left',textvariable=CostofCakes)
txtCostofCakes.grid(row=1, column=1, sticky=W)
lblServiceCharge=Label(f2aa,font=('arial', 16, 'bold'), text="Service Charge", bd=8)
lblServiceCharge.grid(row=2, column=0, sticky=W)
txtServiceCharge=Entry(f2aa, font=('arial', 16, 'bold'), bd=8,
insertwidth=2, justify='left')
txtServiceCharge.grid(row=2, column=1, sticky=W)
#========================================Payment Info===================================
lblPaidTax=Label(f2ab,font=('arial', 16, 'bold'), text="Tax", bd=8)
lblPaidTax.grid(row=0, column=0, sticky=W)
txtPaidTax=Entry(f2ab, font=('arial', 16, 'bold'), bd=8,
insertwidth=2, justify='left', textvariable=PaidTax)
txtPaidTax.grid(row=0, column=1, sticky=W)
lblSubTotal=Label(f2ab,font=('arial', 16, 'bold'), text="Sub Total", bd=8)
lblSubTotal.grid(row=1, column=0, sticky=W)
txtSubTotal=Entry(f2ab, font=('arial', 16, 'bold'), bd=8,
insertwidth=2, justify='left', textvariable=SubTotal)
txtSubTotal.grid(row=1, column=1, sticky=W)
lblTotalCost=Label(f2ab,font=('arial', 16, 'bold'), text="Total", bd=8)
lblTotalCost.grid(row=2, column=0, sticky=W)
txtTotalCost=Entry(f2ab, font=('arial', 16, 'bold'), bd=8,
insertwidth=2, justify='left', textvariable=TotalCost)
txtTotalCost.grid(row=2, column=1, sticky=W)
CostofDrinks.set("")
CostofCakes.set("")
#========================================Buttons=======================
btnTotal = Button(fb2,padx=16,pady=1,bd=4,fg="black",font=('arial', 16,'bold'), width=5,
text="Total ").grid(row=3, column=1)
btnReciept = Button(fb2,padx=16,pady=1,bd=4,fg="black",font=('arial', 16,'bold'), width=5,
text="Reciept ").grid(row=3, column=2)
btnReset = Button(fb2,padx=16,pady=1,bd=4,fg="black",font=('arial', 16,'bold'), width=5,
text="Reset ", command=Reset).grid(row=3, column=3)
btnExit = Button(fb2,padx=16,pady=1,bd=4,fg="black",font=('arial', 16,'bold'), width=5,
text="Exit ",command=qExit).grid(row=3, column=4)
root.mainloop()
After taking some time on this one I noticed several formatting problems. However I am just going to answer the question about the checkbox issue.
You need to change your creation of each checkbox to include command = chkbutton_value.
Take a look at how I would create your Latta check button.
Latta = Checkbutton(f1ab, text="Latte \t", onvalue = 1, offvalue = 0,
font=('arial',18,'bold'), variable = var1, command = chkbutton_value)
NOTE: you need to make sure the function chkbutton_value(): is before the creation of your check buttons because you will get a error otherwise. NameError: name 'chkbutton_value' is not defined

Categories

Resources