Search code examples
pythontkinterpygamecx-freeze

Can regular python input text be carried over when converted to .exe?


I'm working on a program that plays music and also plays a sound effect if you push a button. I was planning on making it so that it could work on any computer, with any sound effect and music file. So I tried making it an .exe with cx_freeze, but it just opens command prompt for a split second and then closes it again. Any tips?

import pygame, time
import tkinter as tk
from pygame.locals import *

print ("WARNING: The program only accepts .wav files.")
musicDir = input("What is the FULL address of the music? (e.g C:\\Me\\Music\\music.wav) ")
soundDir = input("What is the FULL address of the music? (e.g C:\\Me\\Music\\sound.wav) ")

pygame.init() # initialize the pygame

soundObj = pygame.mixer.Sound(musicDir)
soundObj.play(loops=-1)


def playSound():
    soundObj = pygame.mixer.Sound(soundDir)
    soundObj.play()


root = tk.Tk()
frame = tk.Frame(root)
frame.pack()

button = tk.Button(frame, 
               text="QUIT", 
               fg="red",
               command=quit)
button.pack(side=tk.LEFT)
sound = tk.Button(frame,
               text="Play Sound Effect",
               fg="green",
               command=playSound)
sound.pack(side=tk.LEFT)

instruction = tk.Button(root,
             text="Program by Will but really I just wanted another button because it looked cool.", 
             fg="blue")
instruction.pack(side=tk.BOTTOM)
root.mainloop()

Solution

  • I am unable to do the same with cx_freeze, and my code works perfectly without the .exe. I have tried a few things on the internet, to solve the issue but in vain.

    But, if you really want to make a .exe, one of the best options out there is pyinstaller. I use it all the time.

    To install:

    sudo -H pip3 install pyinstaller
    

    To use:

    pyinstaller test_file.py
    

    After that is done, go to the "dist" directory and enter the folder named after your project. In there, there will be quite a lot of files. But, there will be one .exe file, the one that you need. So, click on it to run.

    If you want to input the address of a file don't use an entry, use the following (it looks good, and there will be no errors):

    from tkinter import filedialog as fd
    
    filename = fd.askopenfilename()
    if filename:
        print (filename)
    

    Hope this helps.