简体   繁体   English

通过python中的tkinter按钮运行外部程序

[英]Running an external program through a tkinter button in python

Im new to programming, and really only doing this for a school project. 我是编程新手,实际上只为学校项目这样做。 Im trying to make a GUI that has a series of buttons that when pressed will run a specific emulator. 我试图制作一个具有一系列按钮的GUI,按下该按钮将运行特定的模拟器。 When I try to run this I get a error saying "z26" is undefined. 当我尝试运行此程序时,出现错误消息“ z26”未定义。 Im not quite sure on how to actually define it. 我不太确定如何实际定义它。

from tkinter import *
import os

class Application(Frame):

    def __init__(self, master):
        Frame.__init__(self,master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        self._button = Button(self, text = "Atari", command = self._openFile)
        self._button.grid()
    def _openFile(self):
        os.startfile(z26.exe)

root = Tk()
root.title("Arcade")
root.geometry("200x85")

app = Application(root)

root.mainloop()

The problem is that you are using x26.exe as a literal, and it is getting evaluated as though it were part of the Python program itself. 问题是您将x26.exe用作文字,并且对其进行了评估,就好像它是Python程序本身的一部分一样。

Instead, put the path with quotequotations, to make it a string: 而是将路径用引号引起来,使其成为字符串:

os.startfile('path/z26.exe')

See also the Python documentation for os.startfile(path[, operation]) . 另请参见Python文档os.startfile(path [,operation])

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

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