简体   繁体   English

Tkinter 一对一传递函数

[英]Tkinter Passing Functions one to other

Python: 3.8.0 Python:3.8.0
I Want to make one python file for functions and other for the main code我想制作一个 python 文件用于功能和其他主要代码

EXAMPLE例子
file1.py [MY MAIN FILE] file1.py [我的主文件]

import tkinter as tk
from tkinter import *
import file2

win = tk.Tk()

labeltest = Label(win, text="Hello World")

win.mainloop()

file2.py [the one I want the functions in] file2.py [我想要的功能]

import file1

def testfunc():
labeltest.pack()

and I don't know why it is giving me a error please help我不知道为什么它给我一个错误请帮忙

what you are trying to do is known as circular import which is not allowed in python.to solve this its either you: 1: merge the two files(but you need two program files ) 2: which i think is your solution, you call the import when need in the particular statement.您正在尝试做的被称为循环导入,这在 python 中是不允许的。要解决这个问题,您可以:1:合并两个文件(但您需要两个程序文件) 2:我认为这是您的解决方案,您调用在特定语句中需要时导入。

file1.py becomes file1.py 变成

import tkinter as tk
from tkinter import *

win = tk.Tk()

labeltest = Label(win, text="Hello World")

win.mainloop()

file2 becomes:文件 2 变为:

def testfunc():
    import file1
    file1.labeltest.pack()

Hit me up if you encounter more probs如果您遇到更多问题,请联系我

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM