tkinter 进入 StringVar() 绑定不起作用
tkinter Entry to StringVar() binding do not work
我有一个问题,我不知道为什么..
此屏幕截图总结了所有需要的代码:
Screenshot_Code
请不要找意义,我尽量删减了代码,只是为了展示问题。
我的问题如下:
- 如果我运行main.py
- 然后根据“ui_root.py”点击Button
- 然后在Entry-Field中输入一些文字,然后根据“create_user.py”点击Button
- 然后我的 print 语句是空的,相应的 StringVar() 绑定不起作用
Bu 如果我 运行 “create_user.py” 直接(取消注释 UiCreateUser()),那么我的打印语句会准确打印 content/text来自我的条目小部件。
为什么?我不明白,如果有人能帮助我,我会很感激..在此先感谢。
复制粘贴代码(阅读使用截图,可能比这个好):
main.py
from ui_root import UiRoot
app = UiRoot()
app.mainloop()
ui_root.py
from tkinter import Tk, Button
from pixela_brain import PixelaBrain
class UiRoot(Tk):
def __init__(self):
super().__init__()
self.brain = PixelaBrain()
self.button = Button(self, text="Button", command=self.click)
self.button.pack()
def click(self):
self.brain.create_user()
pixela_brain.py
from popup_windows import create_user
import json
class PixelaBrain:
def __init__(self):
pass
def create_user(self):
self.new_user = create_user.UiCreateUser()
create_user.py
from tkinter import Tk, Entry, Button, StringVar
class UiCreateUser(Tk):
def __init__(self):
super().__init__()
# Entry
self.entry_var = StringVar()
self.entry = Entry(self, textvariable=self.entry_var)
self.entry.pack()
# Button
self.button = Button(self, text="Click", command=self.btn_click)
self.button.pack()
self.mainloop()
def btn_click(self):
print(self.entry_var.get())
# UiCreateUser()
这是一个“答案”,因为 jasonharper 的意见(在上面的评论中)解决了我的问题,我想关闭这个问题。
只需将 class UiCreateUser(Tk) 替换为 class UiCreateUser(Toplevel) 即可解决问题。
非常感谢:)
我有一个问题,我不知道为什么.. 此屏幕截图总结了所有需要的代码: Screenshot_Code
请不要找意义,我尽量删减了代码,只是为了展示问题。
我的问题如下:
- 如果我运行main.py
- 然后根据“ui_root.py”点击Button
- 然后在Entry-Field中输入一些文字,然后根据“create_user.py”点击Button
- 然后我的 print 语句是空的,相应的 StringVar() 绑定不起作用
Bu 如果我 运行 “create_user.py” 直接(取消注释 UiCreateUser()),那么我的打印语句会准确打印 content/text来自我的条目小部件。
为什么?我不明白,如果有人能帮助我,我会很感激..在此先感谢。
复制粘贴代码(阅读使用截图,可能比这个好):
main.py
from ui_root import UiRoot
app = UiRoot()
app.mainloop()
ui_root.py
from tkinter import Tk, Button
from pixela_brain import PixelaBrain
class UiRoot(Tk):
def __init__(self):
super().__init__()
self.brain = PixelaBrain()
self.button = Button(self, text="Button", command=self.click)
self.button.pack()
def click(self):
self.brain.create_user()
pixela_brain.py
from popup_windows import create_user
import json
class PixelaBrain:
def __init__(self):
pass
def create_user(self):
self.new_user = create_user.UiCreateUser()
create_user.py
from tkinter import Tk, Entry, Button, StringVar
class UiCreateUser(Tk):
def __init__(self):
super().__init__()
# Entry
self.entry_var = StringVar()
self.entry = Entry(self, textvariable=self.entry_var)
self.entry.pack()
# Button
self.button = Button(self, text="Click", command=self.btn_click)
self.button.pack()
self.mainloop()
def btn_click(self):
print(self.entry_var.get())
# UiCreateUser()
这是一个“答案”,因为 jasonharper 的意见(在上面的评论中)解决了我的问题,我想关闭这个问题。
只需将 class UiCreateUser(Tk) 替换为 class UiCreateUser(Toplevel) 即可解决问题。
非常感谢:)