gpt4 book ai didi

python - 如何使用户按下按钮后,他们可以在 Tkinter for python 3.7 中输入信息

转载 作者:行者123 更新时间:2023-12-01 08:52:06 29 4
gpt4 key购买 nike

我想知道是否有人可以帮助我,因为我对 Tkinter 还很陌生,并且之前在这里得到过一些很好的帮助。

我的问题是,我正在尝试制作一个预算计算器之类的程序,并且正在使用 Tkinter。我想知道如何获取用户输入,用户可以单击名为“收入”或“费用”的按钮,然后他们可以输入所有数字,并将以表格的形式打印在下面。

我的代码位于下面,任何信息都会真正帮助我,并且到目前为止您可以指出的有关我的代码的任何其他要点将不胜感激!

from time import sleep
from tkinter import *
from tkinter import messagebox, ttk, Tk

root = Tk()

class GUI():

def taskbar(self):

menu = Menu(root)
file = Menu(menu)
root.config(menu=file)
file.add_command(label="Exit", command=self.exit_GUI)
file.add_command(label="Information", command=self.info_popup)
menu.add_cascade(label="File", menu=file)

def Main_Menu(self):
topFrame = Frame(root)
topFrame.pack()
bottomFrame = Frame(root)
bottomFrame.pack(side=BOTTOM)

Income_button = Button(topFrame, text="Enter your incomes", command=self.Income)
Expense_button = Button(topFrame, text="Enter your expenses", command=self.Expense)
Total_button = Button(bottomFrame, text="View Results", command=self.Total)
Income_button.pack()
Expense_button.pack()
Total_button.pack()

def Income(self):
Income_heading = Label(Toplevel(root), text="Please enter the incomes below!", font=("arial", 50, "bold"), fg="blue").pack()

def Expense(self):
Expense_heading = Label(Toplevel(root), text="Please enter the expenses below!", font=("arial", 50, "bold"), fg="blue").pack()

def Total(self):
pass

def exit_GUI(self):
exit()

def info_popup(self):
pass

g = GUI()
g.taskbar()
g.Main_Menu()
#g.Income()
#g.Expense()
g.Total()
g.info_popup()

root.mainloop()

最佳答案

您应该使用类属性来保存值。然后我们可以使用 lambda、entry 和函数的组合来设置这些值。

下面的示例将向您展示如何从类内的顶级窗口设置值。

import tkinter as tk


class GUI():
def __init__(self):
menu = tk.Menu(root)
file = tk.Menu(menu)
root.config(menu=file)
file.add_command(label="Exit", command=self.exit_GUI)
file.add_command(label="Information", command=self.info_popup)
menu.add_cascade(label="File", menu=file)

self.income_var = 0
self.expense_var = 0
self.total_var = 0

top_frame = tk.Frame(root)
top_frame.pack()
bottom_frame = tk.Frame(root)
bottom_frame.pack(side="bottom")

tk.Button(top_frame, text="Enter your incomes", command=self.income).pack()
tk.Button(top_frame, text="Enter your expenses", command=self.expense).pack()
tk.Button(bottom_frame, text="View Results", command=self.total).pack()

def set_vars(self, entry, var):
if var == "income":
self.income_var = entry.get()
print(self.income_var)
if var == "expense":
self.expense_var = entry.get()
print(self.expense_var)
if var == "total":
self.total_var = entry.get()
print(self.total_var)

def income(self):
top = tk.Toplevel(root)
tk.Label(top, text="Please enter the incomes below!", font=("arial", 12, "bold"), fg="blue").pack()
entry = tk.Entry(top)
entry.pack()
tk.Button(top, text="Submit", command=lambda: (self.set_vars(entry, "income"), top.destroy())).pack()

def expense(self):
top = tk.Toplevel(root)
tk.Label(top, text="Please enter the expenses below!", font=("arial", 12, "bold"), fg="blue").pack()
entry = tk.Entry(top)
entry.pack()
tk.Button(top, text="Submit", command=lambda: (self.set_vars(entry, "expense"), top.destroy())).pack()

def total(self):
pass

def exit_GUI(self):
self.destroy()

def info_popup(self):
pass


root = tk.Tk()
g = GUI()
root.mainloop()

关于python - 如何使用户按下按钮后,他们可以在 Tkinter for python 3.7 中输入信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53043306/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com