如何使用 Tkinter 按钮从 python 运行 exe [英] How do I Run an exe from python using a Tkinter button

查看:51
本文介绍了如何使用 Tkinter 按钮从 python 运行 exe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个随机网站生成器,并试图制作它,以便当您单击按钮生成链接时,它也会打开您的浏览器,但我在互联网上找到的所有内容都不起作用.这是我的代码,我非常感谢您的帮助.

I am making a random website generator and am trying to make it so that when you click the button to generate the link it also opens your browser but everything I find on the internet will not work. Here is my code I would really appreciate some help.

from tkinter import *
import random
import tkinter as tk
from tkinter import ttk
import os
NORM_FONT=("timesnewroman", 12)
root = Tk()
class Window(Frame):
    def showtxt(self):
        text=Label(self, text="Please change the file location after os.startfile to the directory of your browser including the browser exe itself")

    def openFile(self):
        os.startfile("C:\Program Files (x86)\Mozilla Firefox\firefox")



    def __init__(self, master=None):
        Frame.__init__(self, master)                 
        self.master = master
        self.init_window()


    def init_window(self):

        self.showtxt


        self.master.title("Random Website Generator")


        self.pack(fill=BOTH, expand=1)


        quitButton = Button(self, command = self.openFile, text="Generate URL", bg = "#009933")

        quitButton.configure(command = lambda: popupmsg ("Please check the shell or command prompt for the URL. And please change the file location after os.startfile to the directory of your browser including the browser .exe itself"))

        quitButton.bind('<ButtonRelease-1>', self.client_exit,)

        quitButton.place(x=150, y=130)

    def client_exit(self, event=None):
        File = open("Random Website.txt",).readlines()
        name=random.choice(File)[:-1]
        print (name)




def popupmsg(msg):
    popup = tk.Tk()
    popup.wm_title("Name Generator")
    label = ttk.Label(popup, text=msg, font=NORM_FONT)
    label.pack(side="top", fill="x", pady=10)
    B1 = ttk.Button(popup, text="OK", command = popup.destroy)
    B1.pack()
    popup.mainloop()






root.geometry("400x300")

app = Window(root)
root.mainloop()  

推荐答案

我想您的代码正在引发 WindowsError: [Error 2] The system cannot find the file specified: 'C:\\Program Files\\Mozilla Firefox (x86)\x0cirefox'.特别要注意这个错误中的 \x0c,它应该\f.

I imagine your code is raising a WindowsError: [Error 2] The system cannot find the file specified: 'C:\\Program Files\\Mozilla Firefox (x86)\x0cirefox'. In particular, notice the \x0c in this error that was supposed to be \f.

您需要对文件路径中的反斜杠进行转义,否则您将在不知不觉中引用转义序列 \f.

You need to escape the backslashes in your file path, or else you will unwittingly reference the escape sequence \f.

os.startfile("C:\\Program Files (x86)\\Mozilla Firefox\\firefox")

或者,您可以使用 原始字符串.

os.startfile(r"C:\Program Files (x86)\Mozilla Firefox\firefox")

这篇关于如何使用 Tkinter 按钮从 python 运行 exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆