CX_Freeze与tkinter gui

时间:2019-03-17 00:02:36

标签: python tkinter cx-freeze

我正在使用cxfreeze来制作我的tkinter gui的可执行文件。我有一些按钮和文本输入,可以在gui上与我的程序进行交互,但是当我尝试在gui后面运行实际程序时似乎没有任何反应。

谁会知道为什么我可以自己运行gui并能正常运行,但是当gui作为可执行文件打开时,它将失去所有功能。这不是所有依赖项都得到解决的问题吗?

嗨,我正在进行此编辑以显示我的tkinter文件和设置文件,希望可以帮助调试。

Setup.py

import sys, os
from cx_Freeze import setup, Executable

PYTHON_INSTALL_DIR = os.path.dirname(sys.executable)
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')

include_files = [(os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'), os.path.join('lib', 'tk86t.dll')),
                 (os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'), os.path.join('lib', 'tcl86t.dll'))]

# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["os"], "includes": ["tkinter"], "include_files": include_files}

# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == "win32":
    base = "Win32GUI"

os.environ['TCL_LIBRARY'] = r'C:\Users\Tyler\AppData\Local\Programs\Python\Python37-32\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Users\Tyler\AppData\Local\Programs\Python\Python37-32\tcl\tk8.6'

setup(  name = "Screenshotter",
        version = "0.1",
        description = "URL Screenshot Utility",
        options = {"build_exe": build_exe_options},
        executables = [Executable("screenshot_gui.py", base=base)])

Gui.py

import random
from tkinter import Tk, Label, Button, Entry, IntVar, StringVar, DISABLED, NORMAL, END, W, E, Radiobutton
from tkinter import filedialog
from six.moves.urllib.request import urlopen
from screenshotter import collect_png

class ScreenshotGUI:
    def __init__(self, master):
        self.master = master
        master.title("URL Screenshotter")

        self.message = "Please enter a url and select a folder to save the screenshots"
        self.label_text = StringVar()
        self.label_text.set(self.message)
        self.label = Label(master, textvariable=self.label_text)
        self.folder = None

        vcmd = master.register(self.validate) # we have to wrap the command
        self.entry = Entry(master)

        self.select_folder = Button(master, text="Select Folder", command=self.get_filename)
        self.enter_button = Button(master, text="Run", command=self.run, state=NORMAL)

        self.label.grid(row=0, column=0, columnspan=2, sticky=W+E)
        self.entry.grid(row=1, column=0, columnspan=2, sticky=W+E)

        self.selector = IntVar()
        self.button0 = Radiobutton(master, text="All Pages", variable=self.selector, value=0)
        self.button1 = Radiobutton(master, text="Main Pages", variable=self.selector, value=1)
        self.button2 = Radiobutton(master, text="Just this page", variable=self.selector, value=2)
        self.button0.grid(row=2, column=0)
        self.button1.grid(row=2, column=1)
        self.button2.grid(row=2, column=2)

        self.select_folder.grid(row=3, column=0)
        self.enter_button.grid(row=3, column=1)

    def get_filename(self):
        self.folder = filedialog.askdirectory()

    def validate(self, new_text):
        return True

    def run(self):
        self.url = str(self.entry.get())
        try:
            urlopen(self.url)
        except Exception as e:
            self.message = "Please enter a valid url\nFull Error:\n" + e
            self.label_text.set(self.message)
            return

        if not self.folder:
            self.message = "Please select a save folder"
            self.label_text.set(self.message)
            return

        self.select_folder.configure(state=DISABLED)
        self.enter_button.configure(state=DISABLED)
        while True:
            try:
                collect_png(self.url, self.folder, self.selector.get())
                break
            except:
                pass

root = Tk()
my_gui = ScreenshotGUI(root)
root.mainloop()

这也是我包含在scipt文件中的模块

from six.moves.urllib.request import urlopen
from bs4 import BeautifulSoup
from lxml import html
import urllib
import imgkit
from time import sleep

0 个答案:

没有答案