Enjoy an ad free experience by logging in. Not a member yet?
Register .
08-21-2012, 05:05 PM
PM User |
#1
New Coder
Join Date: Oct 2011
Posts: 92
Thanks: 38
Thanked 0 Times in 0 Posts
Global Variables not working
Please help, the global list gives the following error.
Code:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python23\lib\lib-tk\Tkinter.py", line 1345, in __call__
return self.func(*args)
File "D:\Desktop Backup\Study Stuff\Year 2_Semester 2\ICT2612_Python\Additional Resources\Saved Files\Window1.py", line 175, in print_save
order += "Salads Ordered: " + salads_ordered[:] + "\n"
TypeError: cannot concatenate 'str' and 'list' objects
Below my complete code and attached the py file.
Code:
# Simple Restaurant GUI
import random
from Tkinter import * #### GUI
# Global Variables
salads_ordered = [] # Does not work
starter_ordered = ""
mains_ordered = ""
global_total = 0
class Application(Frame): #### Class
"""Spur Restaurant Order System"""
def __init__(self, master):
"""Initialising the Frame"""
Frame.__init__(self, master)
self.grid()
self.create_widgets()
# Creating all the Widgets
def create_widgets(self): #### Functions
"""Creating the Labels, buttons, text inputs and outputs"""
# Creating Instruction Label
Label(self, text = "Enter Customers Name and Telephone number.").grid(row = 0, column = 0, columnspan = 3)
# Creating a Customer Label #### Labels
Label(self, text="Customers Name: ").grid(row = 1, column = 0, columnspan = 1, sticky = W)
# Creating a Customer Name Entry Widget #### Entry Widget
self.ent_custN = Entry(self)
self.ent_custN.grid(row = 1, column = 1, columnspan = 1, sticky = W)
# Set focus to the customers name input field
self.ent_custN.focus_set()
# Creating a Telephone Label
Label(self, text="Telephone Number: ").grid(row = 2, column = 0, columnspan = 1, sticky = W)
# Creating a Telephone number Entry Widget
self.ent_telN = Entry(self)
self.ent_telN.grid(row = 2, column = 1, columnspan = 1, sticky = W)
# Creating Salad Label
Label(self, text="Salad Menu: ").grid(row = 3, column = 0, columnspan = 1, sticky = W)
# Creating Salad Check Buttons #### Check Buttons
self.greek_salad = BooleanVar()
Checkbutton(self, text = "Greek Salad", variable = self.greek_salad,
command = self.calc_total).grid(row = 4, column = 0, sticky = W)
self.green_salad = BooleanVar()
Checkbutton(self, text = "Green Salad", variable = self.green_salad,
command = self.calc_total).grid(row = 4, column = 1, sticky = W)
self.coleslaw = BooleanVar()
Checkbutton(self, text = "Coleslaw", variable = self.coleslaw,
command = self.calc_total).grid(row = 4, column = 2, sticky = W)
# Creating Starter Label
Label(self, text="Starter Menu: ").grid(row = 5, column = 0, columnspan = 1, sticky = W)
# Create variable for single Starter meal
self.starter = StringVar()
# Creating Starter Radio Buttons as they can only select one option #### Radio Buttons
Radiobutton(self, text = "Snails", variable = self.starter, value = "snails",
command = self.calc_total).grid(row = 6, column = 0, sticky = W)
Radiobutton(self, text = "Garlic Rolls", variable = self.starter, value = "garlic",
command = self.calc_total).grid(row = 6, column = 1, sticky = W)
Radiobutton(self, text = "Nachos", variable = self.starter, value = "nachos",
command = self.calc_total).grid(row = 6, column = 2, sticky = W)
# Creating Main Course details
Label(self, text="Main Course Menu: ").grid(row = 7, column = 0, columnspan = 1, sticky = W)
# Create variable for single Main Course meal
self.main_course = StringVar()
# Creating Main Course Radio Buttons as they can only select one option
Radiobutton(self, text = "Steak 300g", variable = self.main_course, value = "steak",
command = self.calc_total).grid(row = 8, column = 0, sticky = W)
Radiobutton(self, text = "Line Fish", variable = self.main_course, value = "fish",
command = self.calc_total).grid(row = 8, column = 1, sticky = W)
Radiobutton(self, text = "Beef Burger", variable = self.main_course, value = "burger",
command = self.calc_total).grid(row = 8, column = 2, sticky = W)
# Creating Text Widget to display Order details #### Text Widget
self.txt_display = Text(self, width = 50, height = 8, wrap = WORD)
self.txt_display.grid(row = 9, column = 0, columnspan = 3)
# Creating Total Label
Label(self, text="Order Total: R").grid(row = 10, column = 0, sticky = E)
# Creating Total Entry Widget
self.ent_total = Entry(self)
self.ent_total.grid(row = 10, column = 1, sticky = E)
#### Creating Order button #### Buttons
self.btn_order = Button(self, text="Process Order")
self.btn_order["command"] = self.print_save
self.btn_order.grid(row = 11, column = 2, columnspan = 1, sticky = E)
# Creating Quit button
self.btn_quit = Button(self, text="Quit")
self.btn_quit["command"] = self.quit_pressed
self.btn_quit.grid(row = 11, column = 0, columnspan = 1, sticky = W)
# Creating function to Calculate Total
def calc_total(self):
# Setting the Variables and/or lists and/or dictionaries
SALADS = {'Greek Salad':20.50, 'Green Salad':18.50, 'Coleslaw':20.00} #### Dictionary
STARTERS = {'Snails':40.00, 'Garlic Roll':30.50, 'Nachos':35.00} #### Constants
MAINS = {'Steak 300g':60.00, 'Line Fish':95.50, 'Beef Burger':45.00}
total = 0
global salads_ordered
global starter_ordered
global mains_ordered
global global_total
# Conditional Statement for Salad Menu - Check Buttons
if self.greek_salad.get():
salads_ordered.append("Greek Salad")
total += SALADS['Greek Salad']
if self.green_salad.get():
salads_ordered.append("Green Salad")
total += SALADS['Green Salad']
if self.coleslaw.get():
salads_ordered.append("Coleslaw")
total += SALADS['Coleslaw']
# Conditional Statement for Starter Menu - Radio Buttons
if self.starter.get() == "snails":
total += STARTERS['Snails']
starter_ordered = "Snails"
elif self.starter.get() == "garlic":
total += STARTERS['Garlic Roll']
starter_ordered = "Garlic Roll"
elif self.starter.get() == "nachos":
total += STARTERS['Nachos']
starter_ordered = "Nachos"
# Conditional Statement for Main Course Menu - Radio Buttons
if self.main_course.get() == "steak":
total += MAINS['Steak 300g']
mains_ordered = "Steak 300g."
elif self.main_course.get() == "fish":
total += MAINS['Line Fish']
mains_ordered = "Line Fish."
elif self.main_course.get() == "burger":
total += MAINS['Beef Burger']
mains_ordered = "Beef Burger."
# Display Total in the total entry box
self.ent_total.delete(0, END)
self.ent_total.insert(0, total)
global_total = total
# Writing Order Details to the Text Widget
def print_save(self):
"""Print and Save order details"""
CustName = self.ent_custN.get()
# I inserted this to Capatelize the words
CustName = CustName.title()
TelNumber = self.ent_telN.get()
# Created a Random Order number Which according to me is more random than a
# static number as I am using a slice from the telephone number.
# I am suspecting that in reality this wont be accurate as a number could
# be repeated but I wanted to show off the slice as well.
OrderNumber = random.randrange(int(TelNumber[0:5]))+1 #### Random
# After creating the order number I need to convert to string to print and save
OrderNumber = str(OrderNumber)
# Created a List, not necesary but just to show I understand the principle
order_list = [OrderNumber, CustName, TelNumber] #### List
# Created a variable called order to add details to for print in text widget
order = "Order Number: " + order_list[0] + "\n"
order += "Customer Name: " + order_list[1] + "\n"
order += "Telephone Number: " + order_list[2] + "\n"
order += "Salads Ordered: " + salads_ordered[:] + "\n"
order += "Starters Ordered: " + starter_ordered + "\n"
order += "Main Course Ordered: " + mains_ordered + "\n"
self.txt_display.delete(0.0, END)
self.txt_display.insert(0.0, order)
### Writing order details to text file
# Created conditional statements to orginise the the tab spacing in the text doc
if len(OrderNumber) < 4:
tab1 = "\t\t\t\t"
else:
tab1 = "\t\t\t"
# Created conditional statements to orginise the the tab spacing in the text doc #### Conditional Statements
if len(CustName) < 4:
tab2 = "\t\t\t\t\t\t\t"
elif len(CustName) < 8:
tab2 = "\t\t\t\t\t\t"
elif len(CustName) < 12:
tab2 = "\t\t\t\t\t"
elif len(CustName) < 16:
tab2 = "\t\t\t\t"
elif len(CustName) < 20:
tab2 = "\t\t\t"
elif len(CustName) < 24:
tab2 = "t\t"
else:
tab2 = "\t"
fileName = open("savedData.txt", "a+") #### Writing data to text file
fileName.writelines(OrderNumber + tab1 + CustName + tab2 + TelNumber + "\t\t\t"
+ str(global_total) + "\n")
fileName.close()
# Clear the Input Fields and set focus to the customers name after order placed
self.ent_custN.delete(0, END)
self.ent_telN.delete(0, END)
self.ent_custN.focus_set()
# Quit Function
def quit_pressed(self):
self.master.destroy()
### Main
# Root Window
root = Tk() #### GUI
# Modifying the Window
root.title("Spur - Restaurant")
root.geometry("400x400")
# Instantiate the Application Object
app = Application(root)
# Invoke the root Window
root.mainloop()
Last edited by hans_cellc; 08-21-2012 at 11:16 PM ..
08-21-2012, 05:12 PM
PM User |
#2
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
This isn't PHP code; it looks like python to me.
Unfortunately I don't think there is a lot of python developers here, so I'm not sure what kind of help you'll find.
Moving from PHP forum to Python forum.
08-21-2012, 11:14 PM
PM User |
#3
New Coder
Join Date: Oct 2011
Posts: 92
Thanks: 38
Thanked 0 Times in 0 Posts
Got it to work.
Below my code:
Code:
# Simple Restaurant GUI
import random
from sets import Set
from Tkinter import * #### GUI
# Global Variables
salads_ordered = []
starter_ordered = ""
mains_ordered = ""
total = 0
class Application(Frame): #### Class
"""Spur Restaurant Order System"""
def __init__(self, master):
"""Initialising the Frame"""
Frame.__init__(self, master)
self.grid()
self.create_widgets()
# Creating all the Widgets
def create_widgets(self): #### Functions
"""Creating the Labels, buttons, text inputs and outputs"""
# Logo
Label(self, text="Restaurant Order System", font = "Helvetica 16 bold").grid(row = 0, column = 0, columnspan = 3)
# Creating Instruction Label
Label(self, text = "Enter Customers Name and Telephone number.", font = "Helvetica 10").grid(row = 1, column = 0, columnspan = 3)
# Creating a Customer Label #### Labels
Label(self, text="Customers Name: ", font = "Helvetica 9").grid(row = 2, column = 0, columnspan = 1, sticky = W)
# Creating a Customer Name Entry Widget #### Entry Widget
self.ent_custN = Entry(self)
self.ent_custN.grid(row = 2, column = 1, columnspan = 1, sticky = W)
# Set focus to the customers name input field
self.ent_custN.focus_set()
# Creating a Telephone Label
Label(self, text="Telephone Number: ", font = "Helvetica 9").grid(row = 3, column = 0, columnspan = 1, sticky = W)
# Creating a Telephone number Entry Widget
self.ent_telN = Entry(self)
self.ent_telN.grid(row = 3, column = 1, columnspan = 1, sticky = W)
# Creating Salad Label
Label(self, text="Salad Menu: ", font = "Helvetica 9 bold").grid(row = 4, column = 0, columnspan = 1, sticky = W)
# Creating Salad Check Buttons #### Check Buttons
self.greek_salad = BooleanVar()
Checkbutton(self, text = "Greek Salad", variable = self.greek_salad,
command = self.calc_total).grid(row = 5, column = 0, sticky = W)
self.green_salad = BooleanVar()
Checkbutton(self, text = "Green Salad", variable = self.green_salad,
command = self.calc_total).grid(row = 5, column = 1, sticky = W)
self.coleslaw = BooleanVar()
Checkbutton(self, text = "Coleslaw", variable = self.coleslaw,
command = self.calc_total).grid(row = 5, column = 2, sticky = W)
# Creating Starter Label
Label(self, text="Starter Menu: ", font = "Helvetica 9 bold").grid(row = 6, column = 0, columnspan = 1, sticky = W)
# Create variable for single Starter meal
self.starter = StringVar()
# Creating Starter Radio Buttons as they can only select one option #### Radio Buttons
Radiobutton(self, text = "Snails", variable = self.starter, value = "snails",
command = self.calc_total).grid(row = 7, column = 0, sticky = W)
Radiobutton(self, text = "Garlic Rolls", variable = self.starter, value = "garlic",
command = self.calc_total).grid(row = 7, column = 1, sticky = W)
Radiobutton(self, text = "Nachos", variable = self.starter, value = "nachos",
command = self.calc_total).grid(row = 7, column = 2, sticky = W)
# Creating Main Course details
Label(self, text="Main Course Menu: ", font = "Helvetica 9 bold").grid(row = 8, column = 0, columnspan = 1, sticky = W)
# Create variable for single Main Course meal
self.main_course = StringVar()
# Creating Main Course Radio Buttons as they can only select one option
Radiobutton(self, text = "Steak 300g", variable = self.main_course, value = "steak",
command = self.calc_total).grid(row = 9, column = 0, sticky = W)
Radiobutton(self, text = "Line Fish", variable = self.main_course, value = "fish",
command = self.calc_total).grid(row = 9, column = 1, sticky = W)
Radiobutton(self, text = "Beef Burger", variable = self.main_course, value = "burger",
command = self.calc_total).grid(row = 9, column = 2, sticky = W)
# Creating Text Widget to display Order details #### Text Widget
self.txt_display = Text(self, width = 50, height = 8, wrap = WORD)
self.txt_display.grid(row = 10, column = 0, columnspan = 3)
# Creating Total Label
Label(self, text="Order Total: R", font = "Helvetica 9 bold").grid(row = 11, column = 0, sticky = E)
# Creating Total Entry Widget
self.ent_total = Entry(self)
self.ent_total.grid(row = 11, column = 1, sticky = E)
#### Creating Order button #### Buttons
self.btn_order = Button(self, text="Process Order", font = "Helvetica 9 bold")
self.btn_order["command"] = self.print_save
self.btn_order.grid(row = 12, column = 2, columnspan = 1, sticky = E)
# Creating Quit button
self.btn_quit = Button(self, text="Quit Program", font = "Helvetica 9 bold")
self.btn_quit["command"] = self.quit_pressed
self.btn_quit.grid(row = 12, column = 0, columnspan = 1, sticky = W)
# Creating function to Calculate Total
def calc_total(self):
# Setting the Variables and/or lists and/or dictionaries
SALADS = {'Greek Salad':20.50, 'Green Salad':18.50, 'Coleslaw':20.00} #### Dictionary
STARTERS = {'Snails':40.00, 'Garlic Roll':30.50, 'Nachos':35.00} #### Constants
MAINS = {'Steak 300g':60.00, 'Line Fish':95.50, 'Beef Burger':45.00}
total = 0
saladOrder = []
global salads_ordered
global starter_ordered
global mains_ordered
global global_total
# Conditional Statement for Salad Menu - Check Buttons
if self.greek_salad.get():
saladOrder.append("Greek Salad")
total += SALADS['Greek Salad']
if self.green_salad.get():
saladOrder.append("Green Salad")
total += SALADS['Green Salad']
if self.coleslaw.get():
saladOrder.append("Coleslaw")
total += SALADS['Coleslaw']
# Remove duplicates from List above
salads_ordered = list(Set(saladOrder))
# Conditional Statement for Starter Menu - Radio Buttons
if self.starter.get() == "snails":
total += STARTERS['Snails']
starter_ordered = "Snails"
elif self.starter.get() == "garlic":
total += STARTERS['Garlic Roll']
starter_ordered = "Garlic Roll"
elif self.starter.get() == "nachos":
total += STARTERS['Nachos']
starter_ordered = "Nachos"
# Conditional Statement for Main Course Menu - Radio Buttons
if self.main_course.get() == "steak":
total += MAINS['Steak 300g']
mains_ordered = "Steak 300g."
elif self.main_course.get() == "fish":
total += MAINS['Line Fish']
mains_ordered = "Line Fish."
elif self.main_course.get() == "burger":
total += MAINS['Beef Burger']
mains_ordered = "Beef Burger."
# Display Total in the total entry box
self.ent_total.delete(0, END)
self.ent_total.insert(0, total)
global_total = total
# Writing Order Details to the Text Widget
def print_save(self):
"""Print and Save order details"""
CustName = self.ent_custN.get()
# I inserted this to Capatelize the words
CustName = CustName.title()
TelNumber = self.ent_telN.get()
# Created a Random Order number Which according to me is more random than a
# static number as I am using a slice from the telephone number.
# I am suspecting that in reality this wont be accurate as a number could
# be repeated but I wanted to show off the slice as well.
OrderNumber = random.randrange(int(TelNumber[0:5]))+1 #### Random
# After creating the order number I need to convert to string to print and save
OrderNumber = str(OrderNumber)
# Created a List, not necesary but just to show I understand the principle
order_list = [OrderNumber, CustName, TelNumber] #### List
# Created a variable called order to add details to for print in text widget
order = "Order Number: " + order_list[0] + "\n"
order += "Customer Name: " + order_list[1] + "\n"
order += "Telephone Number: " + order_list[2] + "\n"
order += "Salads Ordered: " + str(salads_ordered) + "\n"
order += "Starters Ordered: " + starter_ordered + "\n"
order += "Main Course Ordered: " + mains_ordered + "\n"
self.txt_display.delete(0.0, END)
self.txt_display.insert(0.0, order)
### Writing order details to text file
# Created conditional statements to orginise the the tab spacing in the text doc
if len(OrderNumber) < 4:
tab1 = "\t\t\t\t"
else:
tab1 = "\t\t\t"
# Created conditional statements to orginise the the tab spacing in the text doc #### Conditional Statements
if len(CustName) < 4:
tab2 = "\t\t\t\t\t\t\t"
elif len(CustName) < 8:
tab2 = "\t\t\t\t\t\t"
elif len(CustName) < 12:
tab2 = "\t\t\t\t\t"
elif len(CustName) < 16:
tab2 = "\t\t\t\t"
elif len(CustName) < 20:
tab2 = "\t\t\t"
elif len(CustName) < 24:
tab2 = "t\t"
else:
tab2 = "\t"
fileName = open("savedData.txt", "a+") #### Writing data to text file
fileName.writelines(OrderNumber + tab1 + CustName + tab2 + TelNumber + "\t\t\t"
+ "R" + str(global_total) + "\n")
fileName.close()
# Clear the Input Fields and set focus to the customers name after order placed #### Clear Fields
self.ent_custN.delete(0, END)
self.ent_telN.delete(0, END)
self.starter = 0
self.main_course = 0
self.greek_salad = 0
self.green_salad = 0
self.coleslaw = 0
self.ent_custN.focus_set()
# Quit Function
def quit_pressed(self):
self.master.destroy()
### Main
# Root Window
root = Tk() #### GUI
# Modifying the Window
root.title("My Restaurant")
root.geometry("400x400")
# Instantiate the Application Object
app = Application(root)
# Invoke the root Window
root.mainloop()
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 08:13 AM .
Advertisement
Log in to turn off these ads.