【python tip】如何调用另外一个.py文件中的函数

本文演示了在同一目录下如何从一个Python文件b.py中导入并使用另一个文件a.py中定义的add函数,实现两个数字相加的操作。

同一文件夹下

b.py调用a.py里的add函数

a.py

def add(x,y):
    return x+y

b.py 

from a import add
b=add(1,2);
print(b);

 

 

Exception in thread Thread-1 (show_warn_tip): Traceback (most recent call last): File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1073, in _bootstrap_inner self.run() File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1010, in run self._target(*self._args, **self._kwargs) File "C:\Users\Administrator\Desktop\1.py", line 42, in show_warn_tip root = tk.Toplevel() ^^^^^^^^^^^^^ File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2697, in __init__ BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2648, in __init__ self.tk.call( RuntimeError: main thread is not in main loop Exception in thread Thread-2 (show_warn_tip): Traceback (most recent call last): File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1073, in _bootstrap_inner self.run() File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1010, in run self._target(*self._args, **self._kwargs) File "C:\Users\Administrator\Desktop\1.py", line 42, in show_warn_tip root = tk.Toplevel() ^^^^^^^^^^^^^ File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2697, in __init__ BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2648, in __init__ self.tk.call( RuntimeError: main thread is not in main loop Exception in thread Thread-3 (show_warn_tip): Traceback (most recent call last): File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1073, in _bootstrap_inner self.run() File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1010, in run self._target(*self._args, **self._kwargs) File "C:\Users\Administrator\Desktop\1.py", line 42, in show_warn_tip root = tk.Toplevel() ^^^^^^^^^^^^^ File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2697, in __init__ BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2648, in __init__ self.tk.call( RuntimeError: main thread is not in main loop Exception in thread Thread-4 (show_warn_tip): Traceback (most recent call last): File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1073, in _bootstrap_inner self.run() File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1010, in run self._target(*self._args, **self._kwargs) File "C:\Users\Administrator\Desktop\1.py", line 42, in show_warn_tip root = tk.Toplevel() ^^^^^^^^^^^^^ File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2697, in __init__ BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2648, in __init__ self.tk.call( RuntimeError: main thread is not in main loop Exception in thread Thread-5 (show_warn_tip): Traceback (most recent call last): File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1073, in _bootstrap_inner self.run() File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1010, in run self._target(*self._args, **self._kwargs) File "C:\Users\Administrator\Desktop\1.py", line 42, in show_warn_tip root = tk.Toplevel() ^^^^^^^^^^^^^ File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2697, in __init__ BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2648, in __init__ self.tk.call( RuntimeError: main thread is not in main loop Exception in thread Thread-6 (show_warn_tip): Traceback (most recent call last): File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1073, in _bootstrap_inner self.run() File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1010, in run self._target(*self._args, **self._kwargs) File "C:\Users\Administrator\Desktop\1.py", line 42, in show_warn_tip root = tk.Toplevel() ^^^^^^^^^^^^^ File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2697, in __init__ BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2648, in __init__ self.tk.call( RuntimeError: main thread is not in main loop Exception in thread Thread-7 (show_warn_tip): Traceback (most recent call last): File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1073, in _bootstrap_inner self.run() File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1010, in run self._target(*self._args, **self._kwargs) File "C:\Users\Administrator\Desktop\1.py", line 42, in show_warn_tip root = tk.Toplevel() ^^^^^^^^^^^^^ File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2697, in __init__ BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2648, in __init__ self.tk.call( RuntimeError: main thread is not in main loop Exception in thread Thread-8 (show_warn_tip): Traceback (most recent call last): File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1073, in _bootstrap_inner self.run() File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1010, in run self._target(*self._args, **self._kwargs) File "C:\Users\Administrator\Desktop\1.py", line 42, in show_warn_tip root = tk.Toplevel() ^^^^^^^^^^^^^ File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2697, in __init__ BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2648, in __init__ self.tk.call( RuntimeError: main thread is not in main loop Exception in thread Thread-9 (show_warn_tip): Traceback (most recent call last): File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1073, in _bootstrap_inner self.run() File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1010, in run self._target(*self._args, **self._kwargs) File "C:\Users\Administrator\Desktop\1.py", line 42, in show_warn_tip root = tk.Toplevel() ^^^^^^^^^^^^^ File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2697, in __init__ BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2648, in __init__ self.tk.call( RuntimeError: main thread is not in main loop Exception in thread Thread-10 (show_warn_tip): Traceback (most recent call last): File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1073, in _bootstrap_inner self.run() File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1010, in run self._target(*self._args, **self._kwargs) File "C:\Users\Administrator\Desktop\1.py", line 42, in show_warn_tip root = tk.Toplevel() ^^^^^^^^^^^^^ File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2697, in __init__ BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2648, in __init__ self.tk.call( RuntimeError: main thread is not in main loop Exception in thread Thread-11 (show_warn_tip): Traceback (most recent call last): File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1073, in _bootstrap_inner self.run() File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1010, in run self._target(*self._args, **self._kwargs) File "C:\Users\Administrator\Desktop\1.py", line 42, in show_warn_tip root = tk.Toplevel() ^^^^^^^^^^^^^ File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2697, in __init__ BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2648, in __init__ self.tk.call( RuntimeError: main thread is not in main loop Exception in thread Thread-12 (show_warn_tip): Traceback (most recent call last): File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1073, in _bootstrap_inner self.run() File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1010, in run self._target(*self._args, **self._kwargs) File "C:\Users\Administrator\Desktop\1.py", line 42, in show_warn_tip root = tk.Toplevel() ^^^^^^^^^^^^^ File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2697, in __init__ BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2648, in __init__ self.tk.call( RuntimeError: main thread is not in main loop Exception in thread Thread-13 (show_warn_tip): Traceback (most recent call last): File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1073, in _bootstrap_inner self.run() File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1010, in run self._target(*self._args, **self._kwargs) File "C:\Users\Administrator\Desktop\1.py", line 42, in show_warn_tip root = tk.Toplevel() ^^^^^^^^^^^^^ File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2697, in __init__ BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2648, in __init__ self.tk.call( RuntimeError: main thread is not in main loop Exception in thread Thread-14 (show_warn_tip): Traceback (most recent call last): File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1073, in _bootstrap_inner self.run() File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1010, in run self._target(*self._args, **self._kwargs) File "C:\Users\Administrator\Desktop\1.py", line 42, in show_warn_tip root = tk.Toplevel() ^^^^^^^^^^^^^ File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2697, in __init__ BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2648, in __init__ self.tk.call( RuntimeError: main thread is not in main loop Exception in thread Thread-15 (show_warn_tip): Traceback (most recent call last): File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1073, in _bootstrap_inner self.run() File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1010, in run self._target(*self._args, **self._kwargs) File "C:\Users\Administrator\Desktop\1.py", line 42, in show_warn_tip root = tk.Toplevel() ^^^^^^^^^^^^^ File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2697, in __init__ BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2648, in __init__ self.tk.call( RuntimeError: main thread is not in main loop Exception in thread Thread-16 (show_warn_tip): Traceback (most recent call last): File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1073, in _bootstrap_inner self.run() File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1010, in run self._target(*self._args, **self._kwargs) File "C:\Users\Administrator\Desktop\1.py", line 42, in show_warn_tip root = tk.Toplevel() ^^^^^^^^^^^^^ File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2697, in __init__ BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2648, in __init__ self.tk.call( RuntimeError: main thread is not in main loop Exception in thread Thread-17 (show_warn_tip): Traceback (most recent call last): File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1073, in _bootstrap_inner self.run() File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1010, in run self._target(*self._args, **self._kwargs) File "C:\Users\Administrator\Desktop\1.py", line 42, in show_warn_tip root = tk.Toplevel() ^^^^^^^^^^^^^ File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2697, in __init__ BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2648, in __init__ self.tk.call( RuntimeError: main thread is not in main loop Traceback (most recent call last): File "C:\Users\Administrator\Desktop\1.py", line 76, in <module> time.sleep(0.12) # 放慢节奏,确保能看清文字 ^^^^^^^^^^^^^^^^ KeyboardInterrupt以上代码运行过后还是有报错请帮我修改后发出来
11-05
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值