按下Tkinter按钮并运行kodi软件?

时间:2018-07-25 08:23:11

标签: python tkinter

我是这个python编程的新手。我正在尝试编写使tkinter按钮在raspberry pi 3中启动外部软件的代码。不幸的是,按下了按钮GUI,但是外部软件或程序没有启动。与按钮GUI链接的外部程序是kodi软件。按下GUI按钮是否可以启动kodi软件?

from tkinter import
import os

class Trying(Frame):

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

    def button(self):
         self.test = Button(self, text="Lauch", command="kodi")
         self.test.grid()

    def kodi():
         os.startfile('/usr/share/applications/kodi.desktop')

root = Tk()
root.title("test kodi")
root.geometry("150x85")

app = Trying(root)

root.mainloop()

1 个答案:

答案 0 :(得分:0)

这是您的固定代码:

from tkinter import *
import os
import subprocess

class Trying(Frame):

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

    def button(self):
        self.test = Button(self, text="Lauch", command=self.kodi)
        self.test.grid()

    def kodi(self):
        print ("Button pressed")
        subprocess.call(['kodi'])

root = Tk()
root.title("test kodi")
root.geometry("150x85")

app = Trying(root)

root.mainloop()

注意:

  • 指定类似command=self.kodi的按钮动作
  • 函数kodi需要接受一个参数def kodi(self)