简体   繁体   English

如果我在一个 function 中创建了小部件,我如何使用 PythonE460F8BEA5A9F5118 在另一个 function 中访问它们

[英]If I created widgets in one function, how can I access them in another function using Python Tkinter?

this is my first project using Tkinter so please excuse me if the question is very simple to solve.这是我第一个使用 Tkinter 的项目,所以如果问题很容易解决,请原谅。 Depending on what option the user chooses from a dropdown, I call a function that creates and places certain widgets (eg an entry) on a frame.根据用户从下拉列表中选择的选项,我调用 function 来创建某些小部件(例如条目)并将其放置在框架上。 Then, when another button is pushed I want to access the text inside this entry.然后,当按下另一个按钮时,我想访问此条目中的文本。 However, this seems to be giving me errors (saying that the widget is undefined) because I want to access a widget which I create when a function is called.但是,这似乎给了我错误(说小部件未定义),因为我想访问我在调用 function 时创建的小部件。

An obvious solution I see is to create all possible widgets I want to use outside the function, and only place them when the function is called.我看到的一个明显的解决方案是创建我想在 function 之外使用的所有可能的小部件,并且仅在调用 function 时放置它们。 This seems quite sloppy and creates many more issues.这似乎很草率,并产生了更多问题。 Is there another fix?还有其他修复吗?

Thanks in advance!提前致谢!

This is the function where I create and place the widgets on the frame.这是 function,我在其中创建小部件并将其放置在框架上。

def loadBook():
    print("book is loaded")
    #Authors
    labelAuth1 = tk.Label(frame, text="Author 1 Name:")
    entryAuth1 = tk.Entry(frame)

    labelAuth1.place(relwidth=0.23, relheight=0.08, rely=0.1)
    entryAuth1.place(relheight=0.08, relwidth=0.18, relx=0.3, rely=0.1)

This is a snippet of a function which uses input from the entry widget I created above:这是 function 的片段,它使用我在上面创建的条目小部件的输入:

def isBook():
    if len(entryAuthSur1.get())==0:
        pass
    else:
        bookString = ""
        bookString += entryAuthSur1.get()

When the second function executes, I get a runtime error that entryAuthSur1 is not defined.当第二个 function 执行时,我收到一个运行时错误,即entryAuthSur1未定义。

All variables inside functions are local.函数内部的所有变量都是局部的。 That means that it is deleted after the function call ends.这意味着它在 function 调用结束后被删除。 As your variable ( entryAuth1 ) wasn't global so it only existed inside the function and was deleted when the loadBook function ended.由于您的变量( entryAuth1 )不是全局变量,因此它仅存在于 function 中,并且在loadBook function 结束时被删除。 This is the working code:这是工作代码:

import tkinter as tk

# Making a window for the widgets
root = tk.Tk()


def loadBook():
    global entryAuth1 # make the entry global so all functions can access it
    print("book is loaded")
    #Authors
    labelAuth1 = tk.Label(root, text="Author 1 Name:")
    entryAuth1 = tk.Entry(root)
    # I will check if the user presses the enter key to run the second function
    entryAuth1.bind("<Return>", lambda e: isBook())

    labelAuth1.pack()
    entryAuth1.pack()

def isBook():
    global entryAuth1 # make the entry global so all functions can access it

    # Here you had `entryAuthSur1` but I guess it is the same as `entryAuth1`
    if len(entryAuth1.get())==0:
        pass
    else:
        bookString = ""
        bookString += entryAuth1.get()
        print(bookString) # I am going to print the result to the screen


# Call the first function
loadBook()

root.mainloop()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 python ZE5BA8B4C39C2087BF134Z26EF5F14Z 中使用一个 function 的数组到另一个 function 中? - How can I use the array of one function into another function in python tkinter? 我如何使用 Tkinter 从 python 中的另一个文件访问 function。 我找不到任何解决我的具体问题的方法 - How do i access a function from another file in python with Tkinter. I can't find any solution to my specific problem 如何使在循环中独立创建的 Python Tkinter 小部件? - How Do I Make Python Tkinter Widgets Created in a Loop Independant? 如何从一个 function 访问另一个变量? - How can i access a variable from one function to another? 我可以定义一个函数在类内创建Tkinter小部件吗? - Can I define a function to create Tkinter widgets inside a class? 如何在另一个 python 脚本中访问函数内的变量 - How can I access a variable inside a function in another python script 当 python 没有返回时,如何访问另一个 function 中的值? - How can I access value in another function when there is no return at python? 如何访问在另一个 function (Python) 中返回的 dataframe - How can I access a dataframe that is returned in another function (Python) Maya Python:当在另一个函数中创建对象时,如何在一个函数中调用和编辑该对象的属性? - Maya Python: How do i call on and edit attributes of an object in one function when that object was created in another function? 如何让导入的模块访问 tkinter 小部件和变量? - How can I have imported modules access tkinter widgets and variables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM