如何做到这一点,以便用户按下按钮后可以在Tkinter for python 3.7中输入其信息

时间:2018-10-29 10:18:00

标签: python python-3.x tkinter

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

在Tkinter中输入信息的小部件是小部件Entry。要获取文本,您可以使用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`