2011-12-14 94 views
2

是否有任何簡單而乾淨的方式在Qt4(實際上是PyQt4)中顯示一個支持文本輸入的消息框,而不僅僅是從預定義列表中選擇一個按鈕?我可以(也可以部分)通過編寫一個自定義類來完成這個任務,只是爲了顯示這樣的對話框,但是沒有更清晰的方法嗎?帶文本輸入的Qt顯示消息框

編輯:我得到它的工作感謝盧卡卡隆。但是以防萬一別人需要這個,我會後在這裏工作如下

from PyQt4.QtGui import QInputDialog 
#This code works only inside a method of a widget or window as self must refer to a 
#valid widget or window to get the correct modality, although we can give None instead 
(text,truth)=QInputDialog.getText(self,"Get text","User name",QLineEdit.Normal,"NoName") 
if truth: 
    #The user has accepted the edit, he/she has clicked OK 
    print text 
else: 
    #The user has not accepted the edit, he/she has clicked Cancel 
    print "No change" 
+0

感謝您的編輯,爲我節省了更多的時間 – James 2016-08-16 17:41:50

回答

5

使用QInputDialog PyQt4的代碼這一點。靜態方法getText可能已經足夠。