問題描述
我對 Python 完全陌生,我剛剛編寫了一小段代碼,用于在 python shell 中打印并請求輸入.它就像一本日記,要求輸入日期,然后打印該日期的條目.我希望將此調用和響應合并到 tkinter GUI 中的文本框中.我想知道如何讓這段代碼在文本框中而不是在 python shell 中執行.
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.
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")
我還想避免將此代碼作為外部文件調用.我想知道如何將此代碼實現到 tkinter GUI 文本框中,而不是如何檢索包含此代碼的文件.主要是因為它是一個如此短的程序,似乎沒有必要.提前感謝您的幫助!
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!
推薦答案
這是一個基本的 Tk 窗口,它將接受月、日和年的輸入
Here is a basic Tk window that will take input for month, day and year
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
編輯
這里是一個顯示日記條目的文本框示例:
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()
在本例中,diary
是日記條目字符串!
in this example diary
is the diary entry string!
祝你好運:)
這篇關于如何在 tkinter、Python 3.2.5 的文本框中打印并讓用戶輸入?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!