【问题标题】:How do I run my Python script using Tkinter?如何使用 Tkinter 运行我的 Python 脚本?
【发布时间】:2015-01-22 14:15:16
【问题描述】:

当我按下 UI 上的按钮时,我试图让我的脚本运行,但不知道如何让它工作。有什么想法吗?

例子:

from tkinter import *

root = Tk()

def OpenPro1():

    print("Hej")

button_1 = Button(root, text = "Hejdå", command = OpenPro1)

button_1.pack()


root.mainloop()

我希望它运行一个程序,而不是打印文本!

【问题讨论】:

  • ...调用相应的函数?如果你想让OpenPro1print("Hej") 以外的事情,那么只需更改它
  • 如果想看结果,看终端窗口
  • 如果要在单独的进程中运行位于单独文件中的程序,请使用子进程,与非 tkinter 进程相同。有几个关于子流程的 SO Q & A。

标签: python user-interface python-3.x tkinter python-idle


【解决方案1】:

您的文本必须包含 unicode 字符:

from Tkinter import *

root = Tk()

def OpenPro1():
    print("Hej")

button_1 = Button(root, text = u'Hejd\xe5', command = OpenPro1)
button_1.pack()

root.mainloop()

【讨论】:

    【解决方案2】:
    from tkinter import *
    
    root = Tk()
    
    def OpenPro1():
    
        print("Hej")
    
        execfile('anyfile.py') #write any file with .py extension.This method is similar to rightclick and open
    
    button_1 = Button(root, text = "Hejdå", command = OpenPro1)
    
    button_1.pack()
    
    
    root.mainloop()
    

    【讨论】:

      猜你喜欢
      • 2018-01-28
      • 2021-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多