在 GUI (Tkinter) 中从函数外部访问变量 Python
Accessing variable from outside the function in GUI (Tkinter) Python
我在 python 中有一个简单的 GUI。我想打印用户从函数外部输入的值。我做的代码如下
def save_BasePath():
Base_Path_info = Base_Path.get()
#print(Base_Path_info)
file_basepath = open("basepath.txt", "w")
file_basepath.write(str(Base_Path_info))
file_basepath.close()
app = Tk()
app.geometry("500x500")
app.title("Python File Handling in Forms")
heading = Label(text="Input Print", fg="black", bg="yellow", width="500",
height="3", font="10")
heading.pack()
Base_Path_text = Label(text="Base_Path :")
Base_Path_text.place(x=155, y=70)
Base_Path = StringVar()
Base_Path_entry = Entry(textvariable=Base_Path, width="30")
Base_Path_entry.place(x=155, y=100)
button_basepath = Button(app, text="Enter Base Path", command=save_BasePath, width="15", height="2", bg="grey")
button_basepath.place(x=175, y=125)
#I need the user input from the function here so that I can use it further
mainloop()
在按下按钮时,我得到了用户输入。我可以从 save_basepath 函数中打印。但我想从外部访问用户输入,以便我可以处理它。感谢任何帮助
我们可以使用全局关键字:
from tkinter import *
def save_BasePath():
global Base_Path_info
Base_Path_info = Base_Path.get()
#print(Base_Path_info)
file_basepath = open("basepath.txt", "w")
file_basepath.write(str(Base_Path_info))
file_basepath.close()
print_it()
def print_it():
print(Base_Path_info)
app = Tk()
app.geometry("500x500")
app.title("Python File Handling in Forms")
heading = Label(text="Input Print", fg="black", bg="yellow", width="500",
height="3", font="10")
heading.pack()
Base_Path_text = Label(text="Base_Path :")
Base_Path_text.place(x=155, y=70)
Base_Path = StringVar()
Base_Path_entry = Entry(textvariable=Base_Path, width="30")
Base_Path_entry.place(x=155, y=100)
button_basepath = Button(app, text="Enter Base Path", command=save_BasePath, width="15", height="2", bg="grey")
button_basepath.place(x=175, y=125)
#I need the user input from the function here so that I can use it further
mainloop()
但请记住,我们想避免这种情况,最好的方法是使用 class.
我会这样做:
from tkinter import *
class myProgram:
def __init__(self):
heading = Label(text="Input Print", fg="black", bg="yellow", width="500",
height="3", font="10")
heading.pack()
Base_Path_text = Label(text="Base_Path :")
Base_Path_text.place(x=155, y=70)
self.Base_Path = StringVar()
Base_Path_entry = Entry(textvariable=self.Base_Path, width="30")
Base_Path_entry.place(x=155, y=100)
button_basepath = Button(app, text="Enter Base Path", command=self.save_BasePath, width="15", height="2", bg="grey")
button_basepath.place(x=175, y=125)
def save_BasePath(self):
self.Base_Path_info = self.Base_Path.get()
file_basepath = open("basepath.txt", "w")
file_basepath.write(str(self.Base_Path_info))
file_basepath.close()
self.print_it()
def print_it(self):
print(self.Base_Path_info)
# Continue your code here...
app = Tk()
app.geometry("500x500")
app.title("Python File Handling in Forms")
myProgram()
mainloop()
我在 python 中有一个简单的 GUI。我想打印用户从函数外部输入的值。我做的代码如下
def save_BasePath():
Base_Path_info = Base_Path.get()
#print(Base_Path_info)
file_basepath = open("basepath.txt", "w")
file_basepath.write(str(Base_Path_info))
file_basepath.close()
app = Tk()
app.geometry("500x500")
app.title("Python File Handling in Forms")
heading = Label(text="Input Print", fg="black", bg="yellow", width="500",
height="3", font="10")
heading.pack()
Base_Path_text = Label(text="Base_Path :")
Base_Path_text.place(x=155, y=70)
Base_Path = StringVar()
Base_Path_entry = Entry(textvariable=Base_Path, width="30")
Base_Path_entry.place(x=155, y=100)
button_basepath = Button(app, text="Enter Base Path", command=save_BasePath, width="15", height="2", bg="grey")
button_basepath.place(x=175, y=125)
#I need the user input from the function here so that I can use it further
mainloop()
在按下按钮时,我得到了用户输入。我可以从 save_basepath 函数中打印。但我想从外部访问用户输入,以便我可以处理它。感谢任何帮助
我们可以使用全局关键字:
from tkinter import *
def save_BasePath():
global Base_Path_info
Base_Path_info = Base_Path.get()
#print(Base_Path_info)
file_basepath = open("basepath.txt", "w")
file_basepath.write(str(Base_Path_info))
file_basepath.close()
print_it()
def print_it():
print(Base_Path_info)
app = Tk()
app.geometry("500x500")
app.title("Python File Handling in Forms")
heading = Label(text="Input Print", fg="black", bg="yellow", width="500",
height="3", font="10")
heading.pack()
Base_Path_text = Label(text="Base_Path :")
Base_Path_text.place(x=155, y=70)
Base_Path = StringVar()
Base_Path_entry = Entry(textvariable=Base_Path, width="30")
Base_Path_entry.place(x=155, y=100)
button_basepath = Button(app, text="Enter Base Path", command=save_BasePath, width="15", height="2", bg="grey")
button_basepath.place(x=175, y=125)
#I need the user input from the function here so that I can use it further
mainloop()
但请记住,我们想避免这种情况,最好的方法是使用 class.
我会这样做:
from tkinter import *
class myProgram:
def __init__(self):
heading = Label(text="Input Print", fg="black", bg="yellow", width="500",
height="3", font="10")
heading.pack()
Base_Path_text = Label(text="Base_Path :")
Base_Path_text.place(x=155, y=70)
self.Base_Path = StringVar()
Base_Path_entry = Entry(textvariable=self.Base_Path, width="30")
Base_Path_entry.place(x=155, y=100)
button_basepath = Button(app, text="Enter Base Path", command=self.save_BasePath, width="15", height="2", bg="grey")
button_basepath.place(x=175, y=125)
def save_BasePath(self):
self.Base_Path_info = self.Base_Path.get()
file_basepath = open("basepath.txt", "w")
file_basepath.write(str(self.Base_Path_info))
file_basepath.close()
self.print_it()
def print_it(self):
print(self.Base_Path_info)
# Continue your code here...
app = Tk()
app.geometry("500x500")
app.title("Python File Handling in Forms")
myProgram()
mainloop()