简体   繁体   中英

Tkinter Python printing user input

I need to take each variable and use it but I'm not sure how print out the result of the outputs. Can someone give me some advice? I'm new with tkinter. Thanks!

import tkinter as tk
from tkinter import simpledialog
from tkinter import *

class MyDialog(simpledialog.Dialog):

    def body(self, master):

        Label(master, text="First:").grid(row=0)
        Label(master, text="Second:").grid(row=1)
        Label(master, text="Third:").grid(row=2)
        Label(master, text="Fourth:").grid(row=3)

        self.e1 = Entry(master)
        self.e2 = Entry(master)
        self.e3 = Entry(master)
        self.e4 = Entry(master)

        self.e1.grid(row=0, column=1)
        self.e2.grid(row=1, column=1)
        self.e3.grid(row=2, column=1)
        self.e4.grid(row=3, column=1)

        return self.e1 # initial focus

    def apply(self):
        first = self.e1.get()
        second = self.e2.get()
        third = self.e3.get()
        fourth =  self.e4.get()


root = tk.Tk()
root.withdraw()
d = MyDialog(root)
print (root)

You need to set self.result inside apply() :

def apply(self):
    first = self.e1.get()
    second = self.e2.get()
    third = self.e3.get()
    fourth =  self.e4.get()
    self.result = (first, second, third, fourth)

Then you can get the result using d.result :

d = MyDialog(root)
print(d.result)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM