pandas 在Excel工作表中存储sg.Text()值的函数?(Python)

amrnrhlw  于 2023-03-11  发布在  Python
关注(0)|答案(1)|浏览(116)

我使用simplepygui和panda来创建一个功能菜单,该菜单将以“汉堡、洋葱、泡菜、饮料”的格式存储名称、地点、用餐时间、费用和订单本身。
有什么方法可以将这些值存储在excel工作表中吗?
如果我使用“总计”旁边的文本框来实际输入计算出的总计,它会存储,但我不能弄清楚如何使它存储在Excel中,而不必手动输入到输入文本框中。
下面是这个程序的代码块:

import PySimpleGUI as sg
import pandas as pd

menu_dictionary = {
    "Cheese":0.50,
    "Sandwhich":1.75,
    "Pickles":0.25,
    "Hot Dog":1.25,
    "Burger": 3.5,
    "Onions": 0.75,
    "Bacon": 1.25,
    "Eggs": 1.00,
    "Fries": 1.25,
    "Chips": 1.25,
    "Salad": 1.25,
    "Potatoes": 1.25,
    "Ranch": 1.25,
    "Ketchup": 1.25,
    "BBQ": 1.25,
    "Drinks": 1.25,
}

Total=0
items = []
Name = ''
sg.theme("DarkTeal9")

EXCEL_FILE= 'MenuTest1.xlsx'
df = pd.read_excel(EXCEL_FILE)

layout = [
    [sg.Text("Welcom to the MAF Menu ")],
    [sg.Text('Name'), sg.InputText(key='Name'),sg.Text('Site'), sg.Combo(["A01", "B01", "C01", "D01", "E01", "F01", "G01", "H01", "I01", "J01", "K01", "L01", "M01", "N01", "O01"], key='Site')],
    [sg.Text("Total $" + str(Total), key='Total'), sg.InputText(key='Cost')],
    [sg.Text('Meal Time'),
                    sg.Checkbox('Breakfast', key='Breakfast'),
                    sg.Checkbox('Lunch', key='Lunch'),
                    sg.Checkbox('Dinner', key='Dinner')],
    [sg.Text("Entrees"), sg.Button("Burger"), sg.Button("Sandwhich"), sg.Button("Hot Dog"), sg.Button("Eggs")],
    [sg.Text("Toppings"), sg.Button("Onions"), sg.Button("Pickles"), sg.Button("Cheese")],
    [sg.Text("Sides"), sg.Button("Fries"), sg.Button("Chips"), sg.Button("Salad"), sg.Button("Potatoes")],
    [sg.Text('Condiments'), sg.Button("Ranch"), sg.Button("Ketchup"), sg.Button("BBQ")],
    [sg.Text("Beverages"), sg.Button("Drinks")],
    [sg.Button("Review"), sg.Text(Name, key='Order')],
    [sg.Submit(), sg.Button("Clear"), sg.Exit()],
]

window = sg.Window('Sample', layout)

def clear_input():

    for key in values:
        window['Order'].update('')
        items = []
        Name = ''
        Total = 0
        window['Total'].update("Total $0")
        window[key]('')
    return None

while True:
    event, values = window.read()

    if event == 'Clear':
        clear_input()

    if event == sg.WIN_CLOSED or event == "Exit":
        break

    if event == 'Submit':
       
        df = df.append(values, ignore_index=True)
        df.to_excel(EXCEL_FILE, index=False)
        sg.popup('Data Stored')
        clear_input()

    if event == "Review":

        order = ", ".join(items)
        order = "{}'s Order: {}".format(Name, order)
        x = Name + order + " for $" + str(Total)

        window['Order'].update(x)

    if event in menu_dictionary:
        Total = Total + menu_dictionary[event]
        if event not in items:
            items.append(event)
        window['Total'].update("Total $" + str(Total))

window.close()

下面是输出:ProgramExcelWorkbook

zphenhs4

zphenhs41#

我还尝试读取sg.Text元素。在我的例子中,它们的值是通过编程设置的,而不是由用户设置的。它们显示给用户,但在提交表单时需要保存。
一直在尝试理解如何使用.Widget属性来访问底层的gui工具包,在我的例子中是tkinter。
print(类型(窗口[元素键].小部件))
生成底层tkinter小部件的类名,但尝试读取属性对我来说不起作用,使用:

w = win["-GPD-"].Widget
   for key in w:
       print(w[key])

调试器显示w的值为:.!toplevel.!frame 2.!frame.!frame 3.!labelframe.!frame 2不是我基于print(type(window[elementKey].Widget))行所期望的tkinter Label对象,上面的print(type(window[elementKey].Widget))行生成:
〈class 'tkinter.标签'〉

相关问题