[英]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.