如何转换函数的输出并将其传递到tkinter中的消息窗口小部件?

时间:2017-11-10 03:50:34

标签: python tkinter

我一直在寻找关于这个问题的回答,但我没有提出任何问题,所以我不妨问。所以我用python制作这个GUI,基本上它是一个可以找到并添加客户到csv文件的GUI。 这就是我现在所拥有的:

from tkinter import *
import csv

class guiVER1:
    def __init__(self, master):
        #Name of the GUI
        self.master = master
        master.title("Packa-Parker's App")
        master.configure(bg="#ffffff")
        self.label = Label(master, text="Packa-Parker's Bank App")
        self.label.configure(bg='white')
        self.label.grid(row=0, column=4)

        myImage = PhotoImage(file ="C:/Users/Shay/Documents/Notebooks/LOGO Term 4.gif")
        self.ImageLabel = Label(master, image=myImage)
        self.ImageLabel.image = myImage
        self.ImageLabel.grid(row=6, column=5)

        self.master.geometry("400x400")

        # Where it stores the Client number
        self.label_text = StringVar()
        self.label_text.set("Find user?")
        self.label = Label(master, textvariable=self.label_text)
        self.label.grid(row=8, column=4)

        #Incase the user inputs something other than an integer/number
        self.label = Label(master, text="Please Type a Valid ID")

        #The number the GUI Starts with
        self.clientNumber= 0

        vcmd = master.register(self.validate)  # we have to wrap the command
        self.entry = Entry(master, validate="key", validatecommand=(vcmd, '%P'))
        self.entry.grid(row=87, column=4)

        self.nameEntry = Entry(master, validate="key", validatecommand=(vcmd, '%P'))
        self.nameEntry.grid(row=4, column=6)

        # The Button to run the find function
        self.updatebutton = Button(master, text="Find", command=self.userFinder)
        self.updatebutton.grid(row=7600, column=2)

        # The button to close the GUI
        self.closedbutton = Button(master, text='close', command=master.quit)
        self.closedbutton.grid(row=7605, column=2)

    def update(self):
        # Updates the number
        self.label_text.set(self.clientNumber)
        self.entry.delete(0,END)

    def updateName(self):
        self.label_text.set(self.nameEntry)
        self.entry.delete(0,END)

    def validate(self, newText):
        # Validate's the number
        if not newText:
            self.clientNumber = 0
            return True

        try:
            self.clientNumber = int(newText)
            print(self.clientNumber)
            return True
        except ValueError:
            print("Please type valid ID")
            return False

    def userFinder(self):
        # Finds the user using the client number
        # inputted by the user
        idx = str(self.clientNumber)
        with open("C:/Users/Shay/Documents/Notebooks/ITN/Assessment/ITN Term 4 Assessment Student Data.csv", 'r') as csvFile:
            csvFileReader = csv.reader(csvFile)
            data = list(csvFileReader)
            del data[:1]
            print(data)
            for d in data:
                if d[0] == idx:
                    print(d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7])
                    break

root = Tk()
my_gui = guiVER1(root)
root.mainloop()

我的问题是我希望userFinder函数的输出显示在单独的窗口中,如消息窗口小部件或文本窗口小部件。现在userFinder函数只在CLI中输出信息,但我希望它只打开另一个包含所有信息的窗口。

我正在使用python 3和一个名为Pycharm的IDE

1 个答案:

答案 0 :(得分:0)

您可以通过运行以下内容来调用基本messagebox

from tkinter import *
from tkinter import messagebox

root = Tk()

messagebox.showinfo("Title", "Content")

root.mainloop()

这意味着在您的代码中,我应该想象您只需要导入messagebox然后替换下面的行。 。

print(d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7])

。 。 。有这样的事情。 。

messagebox.showinfo("Title", (d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7]))