2013-07-27 176 views
0

我正在嘗試製作一個程序,您在輸入框中鍵入文本,然後當您單擊一個按鈕時它將顯示下面的文本,但它不起作用,只要我點擊按鈕,它給了我一個錯誤?爲什麼不會顯示文字

import sys 
from tkinter import * 

def myhello(): 
    text = ment.get() 
    label = Label(text=entry).grid() 
    return 

ment = str() 
root = Tk() 
root.title('Tutorial') 
root.geometry('400x400') 

button = Button(root, text='Button',command = myhello).place(x='160', y='5') 

entry = Entry(textvariable=ment).place(x='5', y= '10 ') 

root.mainloop() 
+0

'地方'通常不被使用。你應該學會使用'pack'和'grid' - 它們更加靈活,並且通常會導致具有良好調整行爲的UI。 –

回答

2

您應該使用StringVar而不是str。您可以同時使用gridplace。選一個。

import sys 
from tkinter import * 

def myhello(): 
    text = ment.get() 
    label['text'] = text 

root = Tk() 
root.title('Tutorial') 
root.geometry('400x400') 

button = Button(root, text='Button',command=myhello).place(x='160', y='5') 
label = Label(root, text='') 
label.place(x=5, y=30) 

ment = StringVar() 
entry = Entry(textvariable=ment).place(x='5', y= '10 ') 

root.mainloop() 
+0

你是一個血腥的救星!謝謝! –

+0

@ user2610429,[接受答案](http://stackoverflow.com/help/accepted-answer)如果我的回答是有幫助的。 – falsetru