我对同时使用tkinter和python并不陌生,我试图创建一个GUI窗口来计算约会之夜的总费用,并为旅行,娱乐等提供不同的选择。我遇到两个主要问题:1.如果用户尝试输入提示,则什么也不会发生。不过,只要用户不尝试离开餐厅小费,该程序就可以运行。我已经尝试过重新格式化,但是我似乎无法弄清楚为什么我的技巧编码方式不起作用。仅当选择了餐厅选项时,才应添加提示。2.此错误可能相对简单,但我无法弄清楚如何编写适当的“退出”按钮。我有几行代码,这是我在在线搜索解决方案后尝试按退出按钮的方法,但是我似乎仍然缺少某些内容。
有很多代码,但是我将其粘贴在这里。我感谢任何建议或反馈:)
#import modules
import tkinter as tk
from tkinter import ttk
'''
calculate cost/make event handler for button push
'''
def calc_cost():
#tax percent to decimal
decimal = tax_result.get() / 100
#if statement for tip
if dinner_result.get() == 30:
before_tax = travel_result.get() + entertainment_result.get() + dinner_result.get + ((int(tip_result.get()) / 100) * dinner_result)
else:
#find total before tax
before_tax = travel_result.get() + entertainment_result.get() + dinner_result.get()
#multiply tax decimal
tax = before_tax * decimal
#add to get total
total_cost = before_tax + tax
#assign
total_label["text"] = "$" + str(round(total_cost, 2))
#create window instance variable
app_window = tk.Tk()
'''
event handler quit button
'''
def close_window():
app_window.destroy
'''
create radio options for travel
'''
#travel value variable
travel_result = tk.IntVar()
#frame shenanigans
travel_frame = ttk.Labelframe(app_window, text = "Travel")
travel_frame.grid(row = 1, column = 1, pady = 10, padx = 10)
#car radio button
car_radio = ttk.Radiobutton(travel_frame, variable = travel_result, value = 5)
car_radio.grid(row = 1, column = 1)
ttk.Label(travel_frame, text = "Car ($5)").grid(row = 1, column = 2, sticky = tk.W)
#bus radio button
bus_radio = ttk.Radiobutton(travel_frame, variable = travel_result, value = 1)
bus_radio.grid(row = 2, column = 1)
ttk.Label(travel_frame, text = "Bus ($1)").grid(row = 2, column = 2, sticky = tk.W)
#uber radio button
uber_radio = ttk.Radiobutton(travel_frame, variable = travel_result, value = 30)
uber_radio.grid(row = 3, column = 1)
ttk.Label(travel_frame, text = "Uber ($30)").grid(row = 3, column = 2, sticky = tk.W)
#limo radio button
limo_radio = ttk.Radiobutton(travel_frame, variable = travel_result, value = 200)
limo_radio.grid(row = 4, column = 1)
ttk.Label(travel_frame, text = "Limo ($200)").grid(row = 4, column = 2, sticky = tk.W)
'''
radio options for entertainment
'''
#entertainment value variable
entertainment_result = tk.IntVar()
#"framing" you for the crime ahaha
#jk just making a frame
entertainment_frame = ttk.Labelframe(app_window, text = "Entertainment")
entertainment_frame.grid(row = 2, column = 1, pady = 10, padx = 10)
#walk radio button
walk_radio = ttk.Radiobutton(entertainment_frame, variable = entertainment_result, value = 0)
walk_radio.grid(row = 1, column = 1)
ttk.Label(entertainment_frame, text = "Walk ($0)").grid(row = 1, column = 2, sticky = tk.W)
#mini golf radio button
golf_radio = ttk.Radiobutton(entertainment_frame, variable = entertainment_result, value = 10)
golf_radio.grid(row = 2, column = 1)
ttk.Label(entertainment_frame, text = "Mini Golf ($10)").grid(row = 2, column = 2, sticky = tk.W)
#movie radio button
#video killed the radio star :( RIP
movie_radio = ttk.Radiobutton(entertainment_frame, variable = entertainment_result, value = 26)
movie_radio.grid(row = 3, column = 1)
ttk.Label(entertainment_frame, text = "Movie ($26)").grid(row = 3, column = 2, sticky = tk.W)
'''
radio options for dinner
'''
#dinner value variable
dinner_result = tk.IntVar()
#dinner frame
dinner_frame = ttk.Labelframe(app_window, text = "Dinner")
dinner_frame.grid(row = 1, column = 2, pady = 10, padx = 10)
#fast food radio button
fast_radio = ttk.Radiobutton(dinner_frame, variable = dinner_result, value = 20)
fast_radio.grid(row = 1, column = 1)
ttk.Label(dinner_frame, text = "Fast Food ($20)").grid(row = 1, column = 2, sticky = tk.W)
#home made radio button
home_radio = ttk.Radiobutton(dinner_frame, variable = dinner_result, value = 15)
home_radio.grid(row = 2, column = 1)
ttk.Label(dinner_frame, text = "Home Made ($15)").grid(row = 2, column = 2, sticky = tk.W)
#restautant radio button
restaurant_radio = ttk.Radiobutton(dinner_frame, variable = dinner_result, value = 30)
restaurant_radio.grid(row = 3, column = 1)
ttk.Label(dinner_frame, text = "Restaurant ($30)").grid(row = 3, column = 2, sticky = tk.W)
'''
tax spinbox
'''
#tax variable
tax_result = tk.IntVar()
#frame
tax_frame = ttk.Labelframe(app_window, text = "Taxes in %")
tax_frame.grid(row = 2, column = 2, pady = 10, padx = 10)
#spinbox
tax_box = tk.Spinbox(tax_frame, from_=0, to=10, width = 5, justify = tk.RIGHT, textvariable = tax_result)
tax_box.grid(row = 1, column = 1, sticky = tk.W, pady = 3)
'''
dinner tip scale
'''
#result variable
tip_result = tk.IntVar()
# frame for the scale
tip_frame = ttk.Labelframe(app_window, text="Restaurant Tip Percent")
tip_frame.grid(row=1, column=3, pady=10, padx=10)
#tip scale range 1-35
tip_scale = tk.Scale(tip_frame, from_=1, to=35, var=tip_result, orient=tk.HORIZONTAL, length=100)
tip_scale.grid(row=1, column=3, sticky=tk.W)
'''
total result display
'''
#label variable
total_cost = tk.IntVar()
result_frame = ttk.Labelframe(app_window, text="Result")
result_frame.grid(row=2, column=3, pady=10, padx=10)
# label for total cost
total_cost = ttk.Label(result_frame, text="Total Cost:")
total_cost.grid(row=1, column=1)
#amount label
total_label = ttk.Label(result_frame, text="---", anchor="center")
total_label.grid(row=2, column=1)
'''
calculate button
'''
calc_button = tk.Button(app_window, text="Calculate Cost", command=calc_cost)
calc_button.grid(row=5, column=2)
'''
quit button
'''
quit_button = tk.Button(app_window, text="Quit", command=close_window)
quit_button.grid(row=5, column=3)
#wait for user
app_window.mainloop()
[此外,对于我提出的所有怪异评论,我深表歉意。我删除了其中的大多数,但是当我这样做时我很累,我认为一切都很有趣。
如何退出:
当我运行代码时,我看到错误消息,其中显示了问题所在和问题所在。您应该阅读它,并思考此行中的错误。并且您还应该添加有问题的此错误消息。