Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用户在按下按钮后可以在Tkinter for python 3.7中输入信息_Python_Python 3.x_Tkinter - Fatal编程技术网

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

如何使用户在按下按钮后可以在Tkinter for python 3.7中输入信息,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我想知道是否有人能帮我,因为我对Tkinter还很陌生,以前在这里得到过一些很好的帮助 我的问题是,我正在尝试制作一个类似预算计算器的程序,并且正在使用Tkinter。我想知道如何获得用户输入,用户可以点击一个叫做“收入”或“费用”的按钮,然后他们可以输入所有的数字,并以表格的形式打印在下面 我的代码在下面,任何信息都会对我有帮助,而且您能指出的关于我的代码的任何其他要点都将不胜感激 from time import sleep from tkinter import * from tkint

我想知道是否有人能帮我,因为我对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()

在Tkinter中输入信息的小部件是小部件条目。要获取文本,可以使用
get()
。例如:

def income_func(self, event):
    text = Income_entry.get() #For get the text into the entry

def Income(self):
    top_lvl = Toplevel(root)
    Income_heading = Label(top_lvl , text="Please enter the incomes below!", font=("arial", 50, "bold"), fg="blue").pack()
    Income_entry = Entry(top_lvl)
    Income_entry.pack()
    Income_entry.bind("<Key-Return>", income_func) #When user press `enter` this call to income_func with argument `event`
def income_func(自我,事件):
text=Income_entry.get()#用于将文本输入条目
def收入(自我):
顶层=顶层(根)
收入标题=标签(顶部,text=“请在下面输入收入!”,font=(“arial”,50,“粗体”),fg=“蓝色”).pack()
收入分录=分录(顶层)
收入_entry.pack()
Income_entry.bind(“,Income_func)#当用户按'enter'时,调用Income_func,参数为'event'`

您应该使用类属性来保存值。然后我们可以使用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()

你必须打包一个条目吗?你会把代码中的.bind行放在哪里?在.get?Hey下面,还有一件事,一旦他们输入数字并按enter键,它就不会做任何事情,它只会停留在文本框Traceback(最近一次调用):File“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/uuuu init_uuu.py”,第1702行,在call return self.func(*args)File“budget.py”第38行,在Income text=Income\u entry.get()AttributeError中:“非类型”对象没有属性“get”,如果你有write Income\u entry=entry(top\u lvl)。pack()这不起作用这很让人困惑,因为我对OOP和Tkinter还不熟悉,但我会尽力解决它。谢谢