[英]Retrieving value from Tkinter Entry box
我试图让 Tkinter 从输入框中返回值,但我似乎无法让它工作。
这是我第一次尝试 Tkinter,s
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\rik\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "n:\regnskab\Afregning\RIK\Udvikling\gui\app2.py", line 43, in step_1
periode_2 = get_period(self)
NameError: name 'get_period' is not defined
import tkinter as tk
from tkinter import ttk
class Mainframe(tk.Tk):
def __init__(self, *args, **kvargs):
super().__init__(*args, **kvargs)
self.title("app")
UserInputFrame(self).grid(row=1, column=0)
ControlsFrame(self).grid(row=2, column=0)
ButtonFrame(self).grid(row=3, column=0)
class UserInputFrame(ttk.Frame):
def __init__(self, container):
super().__init__(container)
self.user_input_year = tk.StringVar(value="2021")
label_entry_year = ttk.Label(self, text="Year")
self.entry_year = ttk.Entry(self, textvariable=self.user_input_year)
label_entry_year.grid(row=2, column=0, sticky="W", padx=10, pady=5)
self.entry_year.grid(row=2, column=1, sticky="EW")
def get_period(self):
print(self.entry_year.get())
class ControlsFrame(ttk.Frame):
def __init__(self, container):
super().__init__(container)
step_1_button = ttk.Button(self, text="Step 1", command=self.step_1)
step_1_button.grid(row=10, column=0, sticky="EW")
def step_1(self):
periode_2 = get_period(self)
print("step 1")
class ButtonFrame(ttk.Frame):
def __init__(self, container):
super().__init__(container)
quit_button = ttk.Button(self, text="Quit", command=quit)
quit_button.grid(row=4, column=1, sticky="EW")
def quit(self):
app.destroy
app = Mainframe()
app.mainloop()
你必须学习OOP:)
get_period()
is not standalone function but it is function inside class UserInputFrame
and you try to access it in class ControlsFrame
. 您不能以这种方式在其他 class 中访问一个 class 的 function。
您必须获取UserInputFrame
的实例,
user_input_frame = UserInputFrame(self)
user_input_frame.grid(row=1, column=0)
将其作为参数发送到ControlsFrame
ControlsFrame(self, user_input_frame).grid(row=2, column=0)
将其保存在 class 变量中
class ControlsFrame(ttk.Frame):
def __init__(self, container, user_input_frame):
super().__init__(container)
self.user_input_frame = user_input_frame
然后你就可以使用它了
def step_1(self):
self.user_input_frame.get_period() # without `self` in `get_period()`
或更好的方法
您应该将UserInputFrame
分配给Mainframe
中的 class 变量
self.user_input_frame = UserInputFrame(self)
self.user_input_frame.grid(row=1, column=0)
然后其他对象(在Mainframe
中创建)可以使用self.master
访问Mainframe
(这是它的 paret/master),然后他们可以访问Mainframe
中的元素,如self.master.userinputframe
periode_2 = self.master.user_input_frame.get_period() # without `self` in `get_period()`
import tkinter as tk
from tkinter import ttk
class Mainframe(tk.Tk):
def __init__(self, *args, **kvargs):
super().__init__(*args, **kvargs)
self.title("app")
self.user_input_frame = UserInputFrame(self)
self.user_input_frame.grid(row=1, column=0)
ControlsFrame(self).grid(row=2, column=0)
ButtonFrame(self).grid(row=3, column=0)
class UserInputFrame(ttk.Frame):
def __init__(self, container):
super().__init__(container)
self.user_input_year = tk.StringVar(value="2021")
label_entry_year = ttk.Label(self, text="Year")
self.entry_year = ttk.Entry(self, textvariable=self.user_input_year)
label_entry_year.grid(row=2, column=0, sticky="W", padx=10, pady=5)
self.entry_year.grid(row=2, column=1, sticky="EW")
def get_period(self):
#print(self.entry_year.get())
return self.entry_year.get()
class ControlsFrame(ttk.Frame):
def __init__(self, container):
super().__init__(container)
step_1_button = ttk.Button(self, text="Step 1", command=self.step_1)
step_1_button.grid(row=10, column=0, sticky="EW")
def step_1(self):
# `self.master` gives access to parent object which here is `Masterframe` (because you used `Masterframe` as `self` when you created `ControlsFrame(self)`
periode_2 = self.master.user_input_frame.get_period() # without `self` in `get_period()`
print("step 1")
class ButtonFrame(ttk.Frame):
def __init__(self, container):
super().__init__(container)
quit_button = ttk.Button(self, text="Quit", command=self.quit)
quit_button.grid(row=4, column=1, sticky="EW")
def quit(self):
app.destroy()
app = Mainframe()
app.mainloop()
顺便说一句:在ButtonFrame
你忘记了self.
在 app.destroy() 中的command=self.quit
和()
app.destroy()
编辑: @acw1668 注意到 function get_period()
不会从条目返回值 - 它只打印它 - 所以它需要return
def get_period(self):
return self.entry_year.get()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.