[英]how do I fix AttributeError: 'Tk' object has no attribute 'open'
I'm trying to make a button that opens another python file我正在尝试制作一个按钮来打开另一个 python 文件
import os
import tkinter as tk
from tkinter import ttk
# root window
root = tk.Tk()
root.geometry('300x200')
root.resizable(False, False)
root.title('juststop')
os.startfile(r'C:\Users\75259\PycharmProjects\pythonProject9\main.py')
open_button = ttk.Button(
root,
text='calculater',
)
open_button.pack(
ipadx=5,
ipady=5,
expand=True
)
root.mainloop()
I have tried a multitude of things but non seem to work我尝试了很多东西,但似乎都不起作用
The way you have your os.startfile
implemented calls the function right away.您实现os.startfile
的方式会立即调用 function。 One way to solve this is to create a function to use it when called and link it to your button.解决此问题的一种方法是创建一个 function 以在调用时使用它并将其链接到您的按钮。 Below is a modification of your example that will only open the file when you press the calculater
button.下面是对您的示例的修改,它只会在您按下calculater
按钮时打开文件。
import os
import tkinter as tk
from tkinter import ttk
# root window
root = tk.Tk()
root.geometry('300x200')
root.resizable(False, False)
root.title('juststop')
def open_file():
os.startfile(r'C:\Users\75259\PycharmProjects\pythonProject9\main.py')
open_button = ttk.Button(root, text='calculater', command=open_file)
open_button.pack(ipadx=5, ipady=5, expand=True)
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.