Home > Blockchain >  Python Ttkinter project not working when converted to .exe
Python Ttkinter project not working when converted to .exe

Time:10-17

i have created a small mining bot for minecraft and for user friendliness (for my friends) i used Tkinter for the app. When i run my app in python it works great, just as expected, but when i turn it into an .exe file (using PyInstaller) it suddenly doesn't work anymore. It just opens a random console and hides the console after a few seconds. To convert my file to .exe i used the following command in the command prompt: "python -m PyInstaller test.py" or "python -m PyInstaller test.py --onefile". Does anybody have any idea whats going on? For anyone interested, here is the code. Any help is appreciated!

import keyboard
import pyautogui

from time import sleep
import tkinter as tk

delay = 5

# program

def INIT():
    window = tk.Tk()
    window.title("Minecraft Mining Bot")

    Stop_Key_Button = tk.StringVar()
    Stop_Key_Button.set("i")

    Greeting_Label = tk.Label(
        text="Hello, user! To start using this program, please make sure to enter your stop key on your keyboard. Goodluck!",
        foreground="white",
        background="black"
    )
    Greeting_Label.pack()

    Set_Key_Label = tk.Label(
        text="Your current stop key is:",
        foreground="white",
        background="black"
    )
    Set_Key_Label.pack()

    Display_Key = tk.Label(
        textvariable=Stop_Key_Button,
        foreground="white",
        background="black"
    )
    Display_Key.pack()

    Set_Key_Input = tk.Button(
        text="Click on this to enter your new key",
        foreground="white",
        background="black",
        command = lambda: SET_KEYBIND(Stop_Key_Button)
    )
    Set_Key_Input.pack()

    Start_Button_Label = tk.Label(
        text="To start the bot, press the button below. You then have 5 seconds to open minecraft and it will start! To stop the bot please press your button you put in.",
        foreground="white",
        background="black"
    )
    Start_Button_Label.pack()

    Start_Button = tk.Button(
        text="Click on this to start the bot with a 5 second delay!",
        foreground="white",
        background="black",
        command = lambda: START_ROBOT(delay, Stop_Key_Button)
    )
    Start_Button.pack()

    window.mainloop()


def SET_KEYBIND(Stop_Key_Button):
    while (1):
        key = keyboard.read_key()
        if (key != None):
            Stop_Key_Button.set(key)
            print(Stop_Key_Button.get())
            return



def START_ROBOT(delay, Stop_Key_Button):

    sleep(delay)

    keyboard.press("ctrl")
    keyboard.press("w")
    pyautogui.mouseDown()

    while(1):
        if(keyboard.is_pressed(Stop_Key_Button.get())):

            keyboard.release("w")
            keyboard.release("ctrl")
            pyautogui.mouseUp()

            break

        sleep(0.1)


if __name__ == "__main__":
    INIT()

CodePudding user response:

use this command to convert it to exe with gui support and without console

pyinstaller.exe --onefile --windowed --icon=icon.ico app.py

if you want tkinter default icon, remove --icon=icon.ico

CodePudding user response:

Aparently it was actually my fault, in short: i used the wrong python script (i was working with multiple but i only wanted to convert 1 file). I accidently kept converting a file with only 5 lines of code and not the file i actually wanted to convert. Thanks for the help because else i didint know all those arguments!

So in the end i only used python -m PyInstaller --onefile --windowed --icon=icon.ico main.py

  • Related