【发布时间】:2019-10-03 10:17:04
【问题描述】:
我正在使用 tkinter 库在 python 中为我自己的项目创建一个 GUI 界面。
在一个框架上,我设置了一个 tk.Entry 让我写一些文本。 我想在下一帧中使用此信息(假设在 tk.Label 中简单) 但我无法获得信息,似乎是因为这两个函数属于不同的类。
试图将 private_key 设为全局,但似乎覆盖了定义。 试图返回 private_key 但我仍然无法访问它,因为我无法在下一个类中调用参数。 尝试在下一节课中再次使用该功能,同样的问题。 尝试在 PVK 类中设置标签,似乎也不起作用
from tkinter import *
# type and size of font
LARGE_FONT = ('MS Serif', 15)
# white writing color
FRONT_COLOR = '#ffffff'
# dark_gray background color
BACKGROUND_COLOR = '#272727'
class Bobby(Tk):
# Used each time the function is called
def __init__(self):
# init tkinter
Tk.__init__(self)
Tk.iconbitmap(self, default='bobby.ico')
Tk.wm_title(self, "Bobby")
Tk.geometry(self, '500x200')
container = Frame(self)
container.pack(side='top', fill='both', expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for f in (PVK, Display):
frame = f(container, self)
self.frames[f] = frame
frame.grid(row=0, column=0, sticky=NSEW)
self.show_frame(Welcome)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class PVK(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
self.configure(background=BACKGROUND_COLOR)
here = Label(self, text="here", font=LARGE_FONT, background=BACKGROUND_COLOR, fg=FRONT_COLOR)
here.grid()
self.pvk = Entry(self, show=" ")
self.pvk.bind('<Return>', self.check)
self.pvk.grid()
def check(self, event):
private_key = int(self.pvk.get()), 11413
bobby.show_frame(Display)
return private_key
class Display(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
self.configure(background=BACKGROUND_COLOR)
#want to display it here
Label(self, text=str(PVK.private_key)).grid()
bobby = Bobby()
bobby.mainloop()
我希望显示带有文本的标签,这意味着我可以使用该变量。 我目前收到 private_key 未定义的错误。
【问题讨论】:
标签: python python-3.x