编译时我的程序不起作用?

时间:2018-01-26 14:18:26

标签: python-3.x tkinter cx-freeze

我做了一个tkinter计划。当我运行它时程序运行良好。但是当我使用cx_Freeze编译它时,当我使用命令python setup.py build时,它运行良好。但是,当我使用命令python setup.py bdist_msi来构建一个简单的installer.it不能正常工作,并没有完成他的功能 我正在使用python 3.6.2 64位

这是我的setup.py

import cx_Freeze
import sys
import os.path

PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
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')

base = None

if sys.platform == 'win32':
    base = "Win32GUI"

executables = [cx_Freeze.Executable("Tasks_Organiser.py", base=base, icon="iccon.ico")]

cx_Freeze.setup(
    name = "Tasks Organiser",
    options = {"build_exe": {"packages":["tkinter","sqlite3"], "include_files":["iccon.ico", "favicon.ico", "Notes.db", os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'),
            os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll')]}},
    version = "0.01",
    description = "GUI APP",
    executables = executables
)

这是我的计划:

from tkinter import *
from tkinter import ttk
from time import ctime
from tkinter import messagebox
import sqlite3


class Organise:

    def __init__(self):
        self.conn = sqlite3.connect("Notes.db")
        self.conn.row_factory = sqlite3.Row
        self.c = self.conn.cursor()
        self.c.execute("CREATE TABLE IF NOT EXISTS organise (TaskName TEXT, timefrom TEXT, timeto TEXT, Comment TEXT, TaskType TEXT)")
        self.conn.commit()

    def add_to_db(self, taskname, From, To, Comment, task_type):
        self.c.execute("INSERT INTO organise (TaskName, timefrom, timeto, Comment, TaskType) VALUES (?, ?, ?, ?, ?)", (taskname, From, To, Comment, task_type))
        self.conn.commit()

    def call_from_db(self):
        call = self.c.execute("SELECT * FROM organise").fetchall()
        return call

    def select_tasks(self):
        select = self.c.execute("SELECT TaskName FROM organise")
        return select

    def delete(self, task):
        self.c.execute("DELETE FROM organise WHERE TaskName ='{}'".format(task))
        self.conn.commit()

    def select_row(self, task):
        self.c.execute("SELECT TaskName, timefrom, timeto, Comment, TaskType FROM organise WHERE TaskName = '%s'"%task)


def add():
    add = AddTask()


root = Tk()
root.title("OrganiseNotes")
root.iconbitmap("favicon.ico")

root.configure(background="#00b4f0")
style = ttk.Style()
style.configure("TLabel", background="#00b3ad")
style.configure("TButton", background="#173701", forebackground="black", relief="flat")
style.map("TButton",
    foreground=[('pressed', '#4b63a1'), ('active', '#00b4f0')],
    background=[('pressed', '!disabled', 'black'), ('active', '#2d74ae')])
style.configure("TRadiobutton", background="#e3e2dd")
style.configure("Treeview", background='#adaca7')

# the organise label with the date of that day(today)

today = ctime()[:10]+ctime()[19:]
start_label = ttk.Label(root, text="Organise {}".format(today), font=("Helvetica", 18, "bold"), compound="center")
start_label.grid(row=0, column=0, columnspan=2, padx=10, pady=10)

# the Add task Button
add_bu = ttk.Button(root, text="Add Task")
add_bu.grid(row=1, column=2, padx=10)
add_bu.config(command=add)

# the delete button
dele_bu = ttk.Button(root, text="Delete")
dele_bu.grid(row=2, column=2, padx=10)


# end the day Button (Production of your day)

endday_bu = ttk.Button(root, text="End The Day")
endday_bu.grid(row=3, column=2, padx=10)


class TvShow:
    def __init__(self):
        self.organ = Organise()
        self.tv = ttk.Treeview(root)
        self.tv.grid(row=1, column=0, columnspan=2, rowspan=3, padx=10, pady=30)
        self.tv.heading("#0", text="ID")
        self.tv.configure(column=("#TaskName", "#From", "#To", "#Comment", "#TaskType"))
        self.tv.heading("#TaskName", text="TaskName")
        self.tv.heading("#From", text="From")
        self.tv.heading("#To", text="To")
        self.tv.heading("#Comment", text="Comment")
        self.tv.heading("#TaskType", text="TaskType")
        self.tv.column("#0", width=50)
        self.tv.column("#TaskName", width=200, anchor="center")
        self.tv.column("#From", width=100, anchor="center")
        self.tv.column("#To", width=100, anchor="center")
        self.tv.column("#Comment", width=300, anchor="center")
        self.i = 1
        self.s = self.organ.call_from_db()

    def add_to_tv(self, taskname, tfrom, tto, comment, task_type):
        self.tv.insert("", "end", text=str(self.i), values=(taskname, tfrom, tto, comment, task_type))
        self.i += 1

    def del_from_tv(self):
        choose = self.tv.selection()
        if len(choose) != 0:
            choose2 = choose[0]
            item_name = self.tv.item(choose2)["values"][0]
            self.organ.delete(item_name)
            self.tv.delete(choose)
            return messagebox.showinfo(title="Task deleted", message="The Task is deleted")
        else:
            return messagebox.showerror(title="Error", message="Select the task you want to delete")


class AddTask:

    def __init__(self):
        self._root = Toplevel()
        self._root.configure(background="#00b4f0")
        self._root.iconbitmap("favicon.ico")

        # the task name section
        ttk.Label(self._root, text="Task Name").grid(row=0, column=0, padx=10, pady=10)
        self.taskname_enter = ttk.Entry(self._root)
        self.taskname_enter.grid(row=0, column=1)


        # the from (time) section
        ttk.Label(self._root, text="From : ").grid(row=1, column=0, padx=10, pady=10)

        ttk.Label(self._root, text="hour").grid(row=1, column=1)
        self.From_enter_hour = ttk.Entry(self._root, width=5)
        self.From_enter_hour.grid(row=1, column=2, padx=20)

        ttk.Label(self._root, text=":").grid(row=1, column=3)

        ttk.Label(self._root, text="minutes").grid(row=1, column=4)
        self.From_enter_min = ttk.Entry(self._root, width=5)
        self.From_enter_min.grid(row=1, column=5, padx=5, pady=5)


        # the to (time) section
        ttk.Label(self._root, text="To:").grid(row=2, column=0)

        ttk.Label(self._root, text="hour").grid(row=2, column=1)
        self.To_enter_hour = ttk.Entry(self._root, width=5)
        self.To_enter_hour.grid(row=2, column=2)

        ttk.Label(self._root, text=":").grid(row=2, column=3)

        ttk.Label(self._root, text="minutes").grid(row=2, column=4)
        self.To_enter_min = ttk.Entry(self._root, width=5)
        self.To_enter_min.grid(row=2, column=5,padx=5, pady=5)



        # the comment section
        ttk.Label(self._root, text="Comment").grid(row=3, column=0, padx=10, pady=10)
        self.comment_enter = Text(self._root, width=20, height=10)
        self.comment_enter.grid(row=3, column=1)

        # the task type section
        ttk.Label(self._root, text="Task Type").grid(row=4, column=0, padx=10, pady=10)
        self.value_type = StringVar()
        self.R1 = ttk.Radiobutton(self._root, text="Very Important", variable=self.value_type, value="Very Important")
        self.R1.grid(row=4, column=1, padx=10, pady=10)

        self.R2 = ttk.Radiobutton(self._root, text="     Important", variable=self.value_type, value="Important")
        self.R2.grid(row=5, column=1, padx=10, pady=10)

        self.R3 = ttk.Radiobutton(self._root, text=" Not Important", variable=self.value_type, value="Not Important")
        self.R3.grid(row=6, column=1)

        # the save button section
        self.save_bu = ttk.Button(self._root, text="Save")
        self.save_bu.grid(row=7, column=4, padx=10, pady=10)
        self.save_bu.config(command=self.busave)

        self._root.mainloop()

    def busave(self):
        try:
            check_from_hour = int(self.From_enter_hour.get())
            check_from_min = int(self.From_enter_min.get())
            check_to_hour = int(self.To_enter_hour.get())
            check_to_min = int(self.To_enter_min.get())
            From_enter= str(check_from_hour)+":"+str(check_from_min)
            To_enter = str(check_to_hour) + ":" + str(check_to_min)
            self.organ = Organise()
            self.organ.add_to_db(self.taskname_enter.get(), From_enter, To_enter, self.comment_enter.get(1.0, "end"), self.value_type.get())
            self.s = self.organ.call_from_db()
            row = self.s[-1]
            tv.add_to_tv(row[0], row[1], row[2], row[3], row[4])

            self.taskname_enter.delete(0, "end")
            self.From_enter_hour.delete(0, "end")
            self.From_enter_min.delete(0, "end")
            self.To_enter_hour.delete(0, "end")
            self.To_enter_min.delete(0, "end")
            self.comment_enter.delete(1.0, "end")
            self.value_type.set("")
        except:
            messagebox.showinfo(title="invalid inputs", message="From and To inputs should be numbers(ex: 3:56)")


class ProductionDay:
    def __init__(self):
        self.organ = Organise()
        self.tasks = self.organ.select_tasks()
        self.tasks_list = []
        for i in self.tasks:
            self.tasks_list.append(i[0])
        self.check = [IntVar(value=0) for i in range(len(self.tasks_list))]
        if len(self.tasks_list) != 0:
            self.root = Toplevel()
            self.root.configure(background="#00b4f0")
            self.root.iconbitmap("favicon.ico")
            self.root.iconbitmap("favicon.ico")
            self.root.title("Your Productivity of Today")
            for x in range(len(self.tasks_list)):
                ttk.Label(self.root, text=self.tasks_list[x]).grid(row=x, column=0, padx = 10, pady=10)
                chech_button = ttk.Checkbutton(self.root, text="done", variable=self.check[x], onvalue=1, offvalue=0).grid(row=x, column=2, padx=10, pady=10)
            save_button = ttk.Button(self.root, text="My Productivity")
            save_button.grid(row=len(self.tasks_list), column=1, padx=150, pady=10)
            save_button.config(command=lambda:self.precentage(self.check))
            self.root.mainloop()
        else:
            messagebox.showerror(title="Error", message="There are no tasks to show")

    def precentage(self, checks):
        checked_ones = [i.get() for i in checks]
        one_count = checked_ones.count(1)
        percentage = (one_count/len(checked_ones))*100
        messagebox.showinfo(title="Productivity of %s"%today, message="Your Productivity is %s" %str(percentage))
        self.root.quit()

def product():
    pro = ProductionDay()

endday_bu.config(command= lambda:product())

tv = TvShow()
dele_bu.config(command=lambda:tv.del_from_tv())
for row in tv.s:
    tv.add_to_tv(row[0], row[1], row[2], row[3], row[4])



root.mainloop()

1 个答案:

答案 0 :(得分:0)

在驱动器C中创建一个文件夹,并将其命名为LOCAL_TO_PYTHON,然后在其中创建另一个文件夹并将其命名为PYTHON35-32。转到安装了python的文件夹或目录并搜索名为tcl的文件夹,然后将其复制并粘贴PYTHON35-32

转到python目录并在文件夹副本tcltk86内搜索DLL文件夹,并将其粘贴到您复制到您创建的文件夹的tcl文件夹中。

然后再次转换程序问题将得到解决。注意我如何命名我创建的文件夹。

see this link to resolve it