2013-09-05 2 views
0

im, работающий по этому вопросу: Напишите программу Python, которая позволит пользователю заказывать элементы из меню в ресторане.Напишите текстовый файл, используя Python

  1. Графический интерфейс должен позволить клиенту войти в его или ее имя и номер телефона .

  2. Система должна сгенерировать случайное число для указания заказа Номер.

  3. Клиент должен иметь возможность выбрать: (1) любые элементы из меню салат, (2) любой один из меню стартера, (3) любой один из основных курсов

  4. Когда пользователь нажимает кнопку заказа, программа должна отображать сведения о клиенте (имя и номер телефона), а также детали заказа, основанные на его или ее выборе.

  5. Должно быть поле, которое отображает общую сумму заказа.

6. Информация, то есть порядковый номер и общее количество заказ должен быть записан в текстовый файл после успешной сделки. Не забудьте записать предыдущую информацию в текстовый файл.

Моя программа работает нормально, но я не знаю, как сделать пулю 6. Который записывает общее количество номера объявления в текстовый файл без перезаписывания.

Вот мой код: # Создать программу меню

import random 

from Tkinter import * 

class App(Frame): 
#GUI application 
    def __init__(self,master): 
    Frame.__init__(self,master) 
    self.grid() 
    self.create_widgets() 

    def create_widgets(self): 
    #Label to welcome the user 
    Label(self, text="Welcome to Costa's restaurant menu" 
     ).grid(row=0, column=0, sticky=W) 

    #Label to request the name of the customer 
    Label(self, text="Customer Name: " 
     ).grid(row=1, column=0, sticky=W) 
    self.ent_cust = Entry(self) 
    self.ent_cust.grid(row=1, column=1, sticky=W) 

    #Label to request the telephone number: 
    Label(self, text="Telephone number: " 
     ).grid(row=1, column=2, sticky=W) 
    self.ent_tel = Entry(self) 
    self.ent_tel.grid(row=1, column=3, sticky=W) 

    #Label to generate a random order number 
    rnd = (int(random.randrange(50)) + 1) 
    Label(self, text=("Your order number " + str(rnd))).grid(row=0, column=2, sticky=W) 
    n = 4 
    #MENU items to choose from 
    #the salads 

    Label(self, text="Select any items from the salad menu: @R30").grid(row=n, column=0, sticky=W) 
    self.has_greensalad = BooleanVar() 

    # the green salad 
    Checkbutton(self, text="Special mixed green salad" 
       , variable=self.has_greensalad).grid(row=(n + 1), column=0, sticky=W) 
    #the blue cheese salad 
    self.has_bluecheese = BooleanVar() 
    Checkbutton(self, text="Blue cheese salad" 
       , variable=self.has_bluecheese).grid(row=(n + 1), column=1, sticky=W) 
    #the greek salad 
    self.has_greek = BooleanVar() 
    Checkbutton(self, text="Greek salad" 
       , variable=self.has_greek).grid(row=(n + 1), column=2, sticky=W) 
    #the starters 
    z = (n + 2) 
    Label(self, text="Select any one of the starter menu: @60:" 
     ).grid(row=z, column=0, sticky=W) 
    #the oysters 
    self.startermenu = StringVar() 
    Radiobutton(self, text="6 oysters", 
       variable=self.startermenu, value="6 oysters" 
       ).grid(row=(z + 1), column=0, sticky=W) 
    #the prawns 
    Radiobutton(self, text="Prawns", 
       variable=self.startermenu, value="Prawns" 
       ).grid(row=(z + 1), column=1, sticky=W) 
    #chicken wings 
    Radiobutton(self, text="Chicken wings", 
       variable=self.startermenu, value="Chicken wings" 
       ).grid(row=(z + 1), column=2, sticky=W) 
    #main course 
    x = (z + 3) 
    Label(self, text="Select any one of the main menu @ R150" 
     ).grid(row=x, column=0, sticky=W) 
    self.maincourse = StringVar() 
    main_courses = ["Hamburger with chips", "Lasagne","Pasta Carbonara","Risotto alla milanese"] 
    column = 0 
    for main in main_courses: 
     Radiobutton(self, 
        text = main, 
        variable=self.maincourse, 
        value = main 
        ).grid(row=(x + 1), column=column, sticky=W) 
     column += 1 
    q = (x + 5) 
    Button(self, text='Place the Order', command=self.determine_order).grid(row=q, column=1, sticky=W) 
    self.txt_order = Text(self, width=75, height=10, wrap=WORD) 
    self.txt_order.grid(row=(q + 1), column=0, columnspan=4) 
    Label(self, text="Total: R", 
     ).grid(row=(q + 11) 
       , column=0, sticky=W) 
    self.txt_total = Text(self, width=8, height=1, wrap=NONE) 
    self.txt_total.grid(row=(q + 11), column=0) 

def determine_order(self): 
    """ Fill text box with order based on user input. """ 
    totalprice = 0 
    salads = 30 
    starters = 60 
    mainmenu = 150 
    name = self.ent_cust.get() 
    tel = self.ent_tel.get() 
    salads = "" 
    if self.has_greensalad.get(): 
     salads += "Special mixed Green Salad\n" 
     totalprice = (totalprice + starters) 
    if self.has_bluecheese.get(): 
     salads += "Blue cheese salad\n" 
     totalprice = (totalprice + starters) 
    if self.has_greek.get(): 
     salads += "Greek Salad\n" 
     totalprice = (totalprice + starters) 
    if (salads == ""): 
     salads = "No salads ordered\n" 
     totalprice = (totalprice + 0) 
    startermenu = self.startermenu.get() 
    if (len(startermenu) == 0): 
     startermenu = "You didn't order a starter" 
    else: 
     totalprice = (totalprice + starters) 
    maincourse = self.maincourse.get() 
    if (len(maincourse) == 0): 
     maincourse = "You didn't order a main course" 
    else: 
     totalprice = (totalprice + mainmenu) 
    order = ((((((((name + " (") + tel) + ") ordered from the salad menu:\n") + salads) + "\nFrom the starters menu:\n ") + startermenu) + "\n\nFrom the main menu:\n") + maincourse) 
    self.txt_order.delete(0.0, END) 
    self.txt_order.insert(0.0, order) 
    self.txt_total.delete(0.0, END) 
    self.txt_total.insert(0.0, float(totalprice)) 


def main(): 
root = Tk() 
root.title("Costa's Restaurant Menu") 
app = App(root) 
root.mainloop()  



main() 
+1

Это очень много вопросов для open ("filename", "a"). Не могли бы вы быть более краткими. – RickyA

ответ

0

Использование open с режимом «а», чтобы добавить данные. Использование режима «w» перезапишет существующие данные.

+0

Могу ли я создать отдельный текстовый файл с помощью блокнота или внутри моей программы? – Unisa

+0

Если файл не существует, эта команда создаст его для вас. – Matthias