此问题似乎难以复制 - 因为我能够在较短的程序中正确执行此操作。我希望的是可能出现问题的一些指导。我已经发布了它正常工作的版本:
import tkinter as tk
from tkinter import *
from tkinter import ttk
class DIS(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "program")
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight = 1)
container.grid_columnconfigure(0, weight = 1)
self.usernameVar = StringVar()
self.frames = {}
for F in (StartPage, contactQues, nowTry, next):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row = 0, column = 0, sticky = "nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
button2 = ttk.Button(self, text = "Here's a Button", command= lambda: controller.show_frame(nowTry))
button2.pack()
class nowTry(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
self.entry1 = Entry(self)
self.entry1.pack()
self.button1 = ttk.Button(self, text = "Yes", command = self.go)
self.button1.pack()
self.entry1.bind("<Return>", self.go)
def go(self, event=None):
print (self.entry1.get())
self.controller.show_frame(contactQues)
class contactQues(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
self.entry1 = Entry(self)
self.entry1.pack(pady=10, padx=10)
self.button1 = ttk.Button(self, text = "Submit", command= self.UsernameSubmit)
self.button1.pack()
self.entry1.bind("<Return>", self.UsernameSubmit)
def UsernameSubmit(self, event=None):
UsernameEntryGet = self.entry1.get()
self.controller.usernameVar.set(UsernameEntryGet)
self.controller.show_frame(next)
class next(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, textvariable = self.controller.usernameVar)
label.pack() ###Label is posted with input correctly
我在主程序中遇到的问题是self.controller.usernameVar
标签在按下Return
键提交时,不会像在此示例中那样发布(根本没有显示)输入。但是,当用鼠标单击submit
按钮时,标签会正确显示。
因此,根据这些信息,我觉得我的bind("<Enter>"...
命令管理错误。我已尝试self.bind...
,self.controller.bind...
,self.entryX.bind...
但未成功。
这个框架的任何想法都可能出错?
答案 0 :(得分:0)
我相信我明白了。问题是,在我的完整程序中,我有多个bind
命令。在我尝试解决此问题时,我有一些entry
提示绑定到controller
,其他提示绑定到entry
本身(例如,self.controller.bind
在几个类中其他人self.entry.bind
。
我将它们全部更改为self.entry.bind
并且显然已修复它 - 这就是为什么此代码段按预期工作的原因。