[英]Python Tkinter, How to pass Variable into mutltiple Functions
EDIT: Thanks to @Osadhi Virochana Jayasinghe Si, Using global "vars" inside the buildwindow() function. makes them readable in the called button function. I also had to fix how to get the Values of the Checkbox and text Widget: Full Fixed code:编辑:感谢@Osadhi Virochana Jayasinghe Si,在 buildwindow() function 中使用全局“变量”。使它们在被调用的按钮 function 中可读。我还必须修复如何获取复选框和文本小部件的值:完全固定代码:
import tkinter as tk
import tkinter.scrolledtext as tkst
from PyQt5.QtWidgets import QApplication # need to install PyQt5 or remove center() Function
def main_window():
window = tk.Tk()
window.title("New Entry")
build_window(window)
center(window) # https://stackoverflow.com/questions/3352918/how-to-center-a-window-on-the-screen-in-tkinter
window.mainloop() # Main Loop, nothing runs after here on Gui
def center(toplevel):
toplevel.update_idletasks()
app = QApplication([])
screen_width = app.desktop().screenGeometry().width()
screen_height = app.desktop().screenGeometry().height()
size = tuple(int(_) for _ in toplevel.geometry().split('+')[0].split('x'))
x = screen_width/2 - size[0]/2
y = screen_height/2 - size[1]/2
toplevel.geometry("+%d+%d" % (x, y))
def build_window(window):
global entry_name, entry_link, entry_xpath, chbox_active, entry_comment, box_var
label_title = tk.Label(window, text="NEW ENTRY")
label_name = tk.Label(window, text="Name:")
entry_name = tk.Entry(window)
label_link = tk.Label(window, text="Link:")
entry_link = tk.Entry(window)
label_xpath = tk.Label(window, text="XPath:")
entry_xpath = tk.Entry(window)
label_active = tk.Label(window, text="Active:")
box_var = tk.IntVar()
chbox_active = tk.Checkbutton(window, variable=box_var, text="Active")
label_comment = tk.Label(window, text="Comment:")
entry_comment = tkst.ScrolledText(window, width=40, height=4, font=("roboto", 8))
botton_cancel = tk.Button(window, text="Done", command=lambda: close_window(window))
button_go = tk.Button(window, text="Run", command=lambda: write_dict(window))
label_title.grid (row=0, column=1, sticky="nwse", padx=2, pady=2)
label_name.grid (row=1, column=0, sticky="e", padx=2, pady=2)
entry_name.grid (row=1, column=1, sticky="nwse", padx=2, pady=2)
label_link.grid (row=2, column=0, sticky="e", padx=2, pady=2)
entry_link.grid (row=2, column=1, sticky="nwse", padx=2, pady=2)
label_xpath.grid (row=3, column=0, sticky="e", padx=2, pady=2)
entry_xpath.grid (row=3, column=1, sticky="nwse", padx=2, pady=2)
label_active.grid (row=4, column=0, sticky="e", padx=2, pady=2)
chbox_active.grid (row=4, column=1, sticky="w", padx=2, pady=2)
label_comment.grid (row=5, column=0, sticky="e", padx=2, pady=2)
entry_comment.grid (row=5, column=1, sticky="w", padx=2, pady=2)
window.grid_rowconfigure(6, minsize=20) # Empty?
botton_cancel.grid(row=7, column=0, sticky="w", padx=2, pady=2) # Cancel Button
button_go.grid(row=7, column=1, sticky="e", padx=2, pady=2) # Write Dict Button
def close_window(window):
window.destroy()
def write_dict(window):
i_dict = {}
i_dict["name"] = entry_name.get()
i_dict["link"] = entry_link.get()
i_dict["xpath"] = entry_xpath.get()
i_dict["active"] = box_var.get()
i_dict["comment"] = entry_comment.get('1.0', tk.END)
print(i_dict)
pass
main_window()
I am trying to make a simple GUI do enter Data into a Dictionary.我正在尝试制作一个简单的 GUI 将数据输入字典。 Most things i find online write the Tkinter straight into the.py, but i want to use a Function to Draw the Window, and another Function to do its stuff once a Button is pressed.
我在网上找到的大多数东西都将 Tkinter 直接写入 .py,但我想使用 Function 来绘制 Window,并在按下按钮后使用另一个 Function 来完成它的工作。
Shorted code:缩短代码:
def main_window():
window = tk.Tk()
build_window(window)
window.mainloop()
def build_window(window):
entry_name = tk.Entry(window)
button_go = tk.Button(window, text="Run", command=lambda: write_dict())
button_go.grid(row=7, column=1, sticky="e", padx=2, pady=2)
def write_dict():
i_dict = {}
i_dict["name"] = entry_name.get()
main_window()
And i am getting AttributeError: module 'tkinter' has no attribute 'entry_name'
.我收到
AttributeError: module 'tkinter' has no attribute 'entry_name'
。 I tried various ways to get window
into write_dict(), but i could never use.get() to read the values inside the Entry Box.我尝试了各种方法将
window
放入 write_dict(),但我永远无法使用 .get() 来读取输入框内的值。
how would i do this?我该怎么做?
Full code:完整代码:
import tkinter as tk
import tkinter.scrolledtext as tkst
from PyQt5.QtWidgets import QApplication
d_list = []
def main_window():
window = tk.Tk()
window.title("New Entry")
build_window(window)
window.mainloop() # Main Loop, nothing runs after here on Gui
def build_window(window):
label_title = tk.Label(window, text="NEW ENTRY")
label_name = tk.Label(window, text="Name:")
entry_name = tk.Entry(window)
label_link = tk.Label(window, text="Link:")
entry_link = tk.Entry(window)
label_xpath = tk.Label(window, text="XPath:")
entry_xpath = tk.Entry(window)
label_active = tk.Label(window, text="Active:")
chbox_active = tk.Checkbutton(window, variable=1, text="Active")
label_comment = tk.Label(window, text="Comment:")
entry_comment = tkst.ScrolledText(window, width=40, height=4, font=("roboto", 8))
botton_cancel = tk.Button(window, text="Done", command=lambda: close_window(window))
button_go = tk.Button(window, text="Run", command=lambda: write_dict())
label_title.grid (row=0, column=1, sticky="nwse", padx=2, pady=2)
label_name.grid (row=1, column=0, sticky="e", padx=2, pady=2)
entry_name.grid (row=1, column=1, sticky="nwse", padx=2, pady=2)
label_link.grid (row=2, column=0, sticky="e", padx=2, pady=2)
entry_link.grid (row=2, column=1, sticky="nwse", padx=2, pady=2)
label_xpath.grid (row=3, column=0, sticky="e", padx=2, pady=2)
entry_xpath.grid (row=3, column=1, sticky="nwse", padx=2, pady=2)
label_active.grid (row=4, column=0, sticky="e", padx=2, pady=2)
chbox_active.grid (row=4, column=1, sticky="w", padx=2, pady=2)
label_comment.grid (row=5, column=0, sticky="e", padx=2, pady=2)
entry_comment.grid (row=5, column=1, sticky="w", padx=2, pady=2)
window.grid_rowconfigure(6, minsize=20) # Empty?
botton_cancel.grid(row=7, column=0, sticky="w", padx=2, pady=2) # Cancel Button
button_go.grid(row=7, column=1, sticky="e", padx=2, pady=2) # Write Dict Button
def close_window(window):
window.destroy()
def write_dict():
global d_list
i_dict = {}
i_dict["name"] = entry_name.get()
i_dict["link"] = entry_link.get()
i_dict["xpath"] = entry_xpath.get()
i_dict["active"] = chbox_active.get()
i_dict["comment"] = entry_comment.get()
print(i_dict)
pass
main_window()
EDIT: The Full Errors are these 2, the first one is with the currently posted code, the second is with passing ´window´ into the button.编辑:完整的错误是这两个,第一个是当前发布的代码,第二个是将“窗口”传递给按钮。
Traceback (most recent call last):
File "C:\Python\Python38\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "C:/Users/Chris/Google Drive/Python/html_new_entry.py", line 50, in <lambda>
button_go = tk.Button(window, text="Run", command=lambda: write_dict())
File "C:/Users/Chris/Google Drive/Python/html_new_entry.py", line 78, in write_dict
i_dict["name"] = entry_name.get()
NameError: name 'entry_name' is not defined
Traceback (most recent call last):
File "C:\Python\Python38\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "C:/Users/Chris/Google Drive/Python/html_new_entry.py", line 50, in <lambda>
button_go = tk.Button(window, text="Run", command=lambda: write_dict(window))
File "C:/Users/Chris/Google Drive/Python/html_new_entry.py", line 78, in write_dict
i_dict["name"] = window.entry_name.get()
File "C:\Python\Python38\lib\tkinter\__init__.py", line 2345, in __getattr__
return getattr(self.tk, attr)
AttributeError: '_tkinter.tkapp' object has no attribute 'entry_name'
Add global entry_name,entry_link,entry_xpath,chbox_active,entry_comment
under the def build_window(window):
It will fix Variable error.在
def build_window(window):
下添加global entry_name,entry_link,entry_xpath,chbox_active,entry_comment
: 它将修复 Variable 错误。
And I fixed all of the issues Here is the Code:我解决了所有问题这是代码:
import tkinter as tk
import tkinter.scrolledtext as tkst
#from PyQt5.QtWidgets import QApplication
d_list = []
def main_window():
window = tk.Tk()
window.title("New Entry")
build_window(window)
window.mainloop() # Main Loop, nothing runs after here on Gui
def build_window(window):
global entry_name,entry_link,entry_xpath,chbox_active,entry_comment,var
var = tk.IntVar()
label_title = tk.Label(window, text="NEW ENTRY")
label_name = tk.Label(window, text="Name:")
entry_name = tk.Entry(window)
label_link = tk.Label(window, text="Link:")
entry_link = tk.Entry(window)
label_xpath = tk.Label(window, text="XPath:")
entry_xpath = tk.Entry(window)
label_active = tk.Label(window, text="Active:")
chbox_active = tk.Checkbutton(window, variable=var, text="Active")
label_comment = tk.Label(window, text="Comment:")
entry_comment = tkst.ScrolledText(window, width=40, height=4, font=("roboto", 8))
botton_cancel = tk.Button(window, text="Done", command=lambda: close_window(window))
button_go = tk.Button(window, text="Run", command=lambda: write_dict())
label_title.grid (row=0, column=1, sticky="nwse", padx=2, pady=2)
label_name.grid (row=1, column=0, sticky="e", padx=2, pady=2)
entry_name.grid (row=1, column=1, sticky="nwse", padx=2, pady=2)
label_link.grid (row=2, column=0, sticky="e", padx=2, pady=2)
entry_link.grid (row=2, column=1, sticky="nwse", padx=2, pady=2)
label_xpath.grid (row=3, column=0, sticky="e", padx=2, pady=2)
entry_xpath.grid (row=3, column=1, sticky="nwse", padx=2, pady=2)
label_active.grid (row=4, column=0, sticky="e", padx=2, pady=2)
chbox_active.grid (row=4, column=1, sticky="w", padx=2, pady=2)
label_comment.grid (row=5, column=0, sticky="e", padx=2, pady=2)
entry_comment.grid (row=5, column=1, sticky="w", padx=2, pady=2)
window.grid_rowconfigure(6, minsize=20) # Empty?
botton_cancel.grid(row=7, column=0, sticky="w", padx=2, pady=2) # Cancel Button
button_go.grid(row=7, column=1, sticky="e", padx=2, pady=2) # Write Dict Button
def close_window(window):
window.destroy()
def write_dict():
global d_list
i_dict = {}
v = ""
i_dict["name"] = entry_name.get()
i_dict["link"] = entry_link.get()
i_dict["xpath"] = entry_xpath.get()
i_dict["active"] = var.get()
i_dict["comment"] = entry_comment.get('1.0', 'end-1c')
print(i_dict)
pass
main_window()
Now you can get checkbox status and comment box status too.现在您也可以获得复选框状态和评论框状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.