如何在 tkinter、Python 3.2.5 的文本框中打印并让用户输入?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17457178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:16:01  来源:igfitidea点击:

How do I print and have user input in a text box in tkinter, Python 3.2.5?

pythoninputtextboxtkinter

提问by HowardKRictor

I am completely new to Python, and I just wrote a short bit of code that prints and asks for input in the python shell. It works like a diary where it asks for a date and then prints the entries for that date. I was hoping to incorporate this call and response into a text box in a tkinter GUI. I am wondering how to get this bit of code to perform in the text box instead of in the python shell.

我对 Python 完全陌生,我只是写了一小段代码,在 python shell 中打印并要求输入。它就像一本日记,它要求输入日期,然后打印该日期的条目。我希望将此调用和响应合并到 tkinter GUI 中的文本框中。我想知道如何让这段代码在文本框中而不是在 python shell 中执行。

month = int(float(input("Month(MM): ")))
day = int(float(input("Day(DD): ")))
year = int(float(input("Year(YYYY): ")))

print(str(month)+"/"+str(day)+"/"+str(year))

noEntry = True

if month == 1 and day == 2 and year == 3456:
    noEntry = False
    print("Text")
if month == 7 and day == 8 and year == 9012:
    noEntry = False
    print("More Text")
if noEntry:
    print("No Entry Found")

I would also like to avoid calling for this code as an outside file. I want to know how to implement this code into a tkinter GUI text box, not how to retrieve a file which contains this code. Mostly because it is such a short program and it seems unnecessary. Thanks for the help in advance!

我还想避免将此代码作为外部文件调用。我想知道如何将此代码实现到 tkinter GUI 文本框中,而不是如何检索包含此代码的文件。主要是因为它是一个如此短的程序,似乎没有必要。我在这里先向您的帮助表示感谢!

回答by Serial

Here is a basic Tk window that will take input for month, day and year

这是一个基本的 Tk 窗口,它将接受月、日和年的输入

from Tkinter import *

root = Tk()


label1 = Label( root, text="Month(MM)")
E1 = Entry(root, bd =5)

label2 = Label( root, text="Day(DD)")
E2 = Entry(root, bd =5)

label3 = Label( root, text="Year(YYYY)")
E3 = Entry(root, bd =5)

def getDate():
    print E1.get()
    print E2.get()
    print E3.get()

submit = Button(root, text ="Submit", command = getDate)

label1.pack()
E1.pack()
label2.pack()
E2.pack()
label3.pack()
E3.pack()
submit.pack(side =BOTTOM) 
root.mainloop()

when you click submit it prints the month day and year and im sure you can figure it out from there

当您单击提交时,它会打印月日和年,我确定您可以从那里弄清楚

EDIT

编辑

here is an example of a text box to display the diary entry:

这是显示日记条目的文本框示例:

from Tkinter import *

root = Tk()
text = Text(root)
text.insert(INSERT, diary)
text.pack()

root.mainloop()

in this example diaryis the diary entry string!

在这个例子中diary是日记条目字符串!

Good Luck :)

祝你好运 :)