我通過按鈕觸發器創建了一個 tkinter 視窗(如圖所示的“window3”)。我想要一個函式,它從 'window3' 上的 'KonuIcerik' ScrolledText 獲取資料,然后將其發送到函式 'def Finish():'
我知道我的問題是關于使用函式,但我無法修復它。我需要一點幫助
錯誤:“KonuIcerik”未定義
def KonuOlusturButtonu():
window3= Toplevel(root)
window3.geometry(f'{root_width}x{root_height} {center_x} {center_y}')
window3.title("Konu Olu?tur")
window3.resizable(False, False)
window3.configure(background='#0a0a0a')
# Create ScrolledText widget
KonuIcerik = ScrolledText(window3, width=96, height=30)
KonuIcerik.place(relx=0.5, rely=0.55, anchor=CENTER)
# Create Button widget
button_konubitir = Button(window3, text ="Bitir ve Yay?nla", command = Finish())
button_konubitir.config( height = 2, width = 30 )
button_konubitir.place(relx=0.5, rely=0.92, anchor=CENTER)
def Finish():
KonuIcerikText = KonuIcerik.get()
print(KonuIcerikText);
uj5u.com熱心網友回復:
首先更改函式以接受引數:
def Finish(widget):
KonuIcerikText = widget.get('0.0', 'end')
print(KonuIcerikText);
然后通過創建 alambda稍后呼叫它并傳遞widget函式要使用的引數來更改命令以獲取文本:
button_konubitir = Button(window3, text="Bitir ve Yay?nla", command=lambda: Finish(KonuIcerik))
也可以看看:
為什么在宣告時命令系結到按鈕或事件執行?
我強烈建議遵循PEP 8 - Python 代碼風格指南。函式名和變數名應該在
snake_case,類名應該在CapitalCase. 沒有足夠的空間周圍=,如果它被用作關鍵字引數的一部分,(func(arg='value')),但周圍有空間,=如果是用于分配的值(variable = 'some value')。在運算子周圍留出空間(-/等value = x y:(此處除外value = x y))。在函式和類宣告周圍有兩個空行。類方法周圍有一個空行。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/346961.html
下一篇:將多個字串輸入轉換為int
