我有一个字符串“currentMessage”和一个显示它的标签。 我有一个顶层小部件,它包含一个文本小部件,为“currentMessage”提供新值:
from tkinter import *
from tkinter import ttk
root = Tk()
mainFrame = ttk.Frame(root)
mainFrame.grid()
currentMessage = 'current Message'
ttk.Label(mainFrame, text = currentMessage).grid(padx = 10, pady = 10)
def updateCurrentMessage(popupWindow):
currentMessage = popupWindow.textBox.get(0.0, END)
def changeValues():
popup = Toplevel(mainFrame)
popup.grid()
textBox = Text(popup, width = 20, height = 5)
textBox.grid(column = 0, row = 0)
textBox.insert(END, 'new message here')
b = ttk.Button(popup, command = lambda: updateCurrentMessage(popup))
b.grid(column = 0, row = 1, padx = 5, pady = 5)
b['text'] = 'Update'
theButton = ttk.Button(mainFrame, command = changeValues, text = 'Click')
theButton.grid(padx = 10, pady = 10)
mainFrame.mainloop()
我尝试使用以下函数获取顶层“textBox”文本小部件的内容:
^{pr2}$但我有个错误
'Toplevel' object has no attribute 'textBox'
那么我如何访问小部件“textBox”的内容,它是“popup”的子小部件(这个顶级小部件只有在调用changeValues()函数时才会创建)?在
确实有一种方法,像这样:
你可以选择任何你想要的名字。在
我想这可能就是你想要的,尽管我只是猜测,因为你在寻求一个你认为你有特定问题的解决方案,但是如果我是你,我会重新思考我到底想做什么:
相关问题 更多 >
编程相关推荐