Python Tkinter - 处理用户输入的数据

时间:2017-11-08 10:25:44

标签: python tkinter

我想用Tkinter GUI在Python中创建一个程序,我希望它从用户那里获取字符串输入,然后我想对这些字符串进行一些操作 - 在这种情况下,我想混合两个单词的部分并获得一个新词。如何处理用户输入的数据并使用它来接收结果?以下是我的代码。我无法找到这个问题的答案,我尝试过的任何工作都没有。

from Tkinter import *    
class Window(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master = master
        self.init_window()      
    def init_window(self):
        self.master.title("Mix words")
        self.pack(fill=BOTH, expand=1)
        menu = Menu(self.master)
        self.master.config(menu=menu)
        entryLbl1 = Label(self, text="Write the first word: ")
        entryLbl1.pack()
        self.entrytext1 = StringVar()
        Entry(self, textvariable=self.entrytext1).pack()
        self.buttontext1 = StringVar()
        self.buttontext1.set("OK")
        Button(self, textvariable=self.buttontext1, command=self.clicked1).pack()
        self.label1 = Label(self, text="")
        self.label1.pack()
        global user_entry1 
        user_entry1 = self.entrytext1.get()
        entryLbl2 = Label(self, text="Write the second word: ")
        entryLbl2.pack()
        self.entrytext2 = StringVar()
        Entry(self, textvariable=self.entrytext2).pack()
        self.buttontext2 = StringVar()
        self.buttontext2.set("OK")
        Button(self, textvariable=self.buttontext2, command=self.clicked2).pack()
        self.label2 = Label(self, text="")
        self.label2.pack()
        global user_entry2
        user_entry2 = self.entrytext2.get()
        entryLbl3 = Label(self, text="Result: ")
        entryLbl3.pack()
        self.buttontext3 = StringVar()
        self.buttontext3.set("Result")
        Button(self, textvariable=self.buttontext1, command=self.clicked3).pack()
        self.label3 = Label(self, text="")
        self.label3.pack()        
    def clicked1(self):
        input = self.entrytext1.get()
        self.label1.configure(text=input)
    def clicked2(self):
        input = self.entrytext2.get()
        self.label2.configure(text=input)
    def clicked3(self):
        self.user_entry1 = user_entry1
        self.user_entry2 = user_entry2
        first2a = user_entry1[0:2]
        rest_a = user_entry1[2:]
        first2b = user_entry2[0:2]
        rest_b = user_entry2[2:]
        input = first2b + rest_a + " " + first2a + rest_b
        self.label3.configure(text=input)
root = Tk()
root.iconbitmap("py.ico")
root.geometry("600x300")
app = Window(root)
root.mainloop()

1 个答案:

答案 0 :(得分:1)

您需要Entry()个对象。

以下内容将显示两个Entry小部件和一个Button。 按下按钮时,两个Entry对象的内容将打印到控制台:

import sys


# Determine if you're running Python 3
is_py_3 = sys.version[0] == '3'


# Import Tkinter for the correct version of Python
if is_py_3:
    from tkinter import Button, Entry, Tk
else:
    from Tkinter import Button, Entry, Tk

class GUI:

    def __init__(self):

        # Set up the "Root" or "Parent" of the window.
        self.root = Tk()

        # Set up two "Entry" widgets.
        self.entry1 = Entry(self.root)
        self.entry1.insert(0, "Enter something here.")

        self.entry2 = Entry(self.root)
        self.entry2.insert(0, "and here...")

        # Set up a button to handle the event.
        self.button = Button(self.root, text="CLICK ME", command=self.onClicked)

        self.entry1.pack()
        self.entry2.pack()
        self.button.pack()

    def onClicked(self):
        # Print the contents of the entry widgets.
        s1 = self.entry1.get()
        s2 = self.entry2.get()

        print(s1, s2)


app = GUI()
app.root.mainloop()