简体   繁体   English

我该如何修复 AttributeError: 'Tk' object has no attribute 'open'

[英]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.

相关问题 如何修复 AttributeError: 'bytes' object has no attribute 'encode'? - How do I fix AttributeError: 'bytes' object has no attribute 'encode'? 如何修复此 AttributeError: 'SubRequest' object 没有属性 'getfuncargvalue'? - How do I fix this AttributeError: 'SubRequest' object has no attribute 'getfuncargvalue'? 我该如何解决这个 AttributeError: 'NoneType' object has no attribute 'text'? - How do i fix this AttributeError: 'NoneType' object has no attribute 'text'? 如何修复 AttributeError: 'NoneType' object 没有属性 'lower'? - How do i fix AttributeError: 'NoneType' object has no attribute 'lower'? AttributeError: 对象没有属性“tk” - AttributeError: object has no attribute 'tk' 我收到一条错误消息,指出 AttributeError: 'str' object has no attribute 'read',我不知道如何修复它 - I'm getting an error that says AttributeError: 'str' object has no attribute 'read' and i do not know how to fix it AttributeError:'str'对象没有属性'tk' - AttributeError: 'str' object has no attribute 'tk' AttributeError: 'MeshPlotter' object 没有属性 'tk' - AttributeError: 'MeshPlotter' object has no attribute 'tk' AttributeError:“模块”对象没有属性“ tk” - AttributeError: 'module' object has no attribute 'tk' Tkinter AttributeError:对象没有属性“ tk” - Tkinter AttributeError: object has no attribute 'tk'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM