Home > Blockchain >  Why does it open the file in the editor instead of running it?
Why does it open the file in the editor instead of running it?

Time:11-24

so I've been working on a Tkinter project to get better at Python, and I've made a number guesser game (number_guesser.py) and now im trying to make a menu window with buttons. Depending on what button you click it will forward you to the specified game window and close the main window.

This is my current code in the main.py file (menu window)

import os
import random

main = tk.Tk()
main.title("Minigames")
main.geometry("450x400")
main.resizable(width= False, height= False)

def forwardtoo():
    os.startfile('C:/Users/Windows/PycharmProjects/GUI with minigames/number_guesser.py')
    main.destroy()




b1 = tk.Button(main, text="Number Guesser", command=forwardtoo)
b1.place(x = 170, y = 250)

main.mainloop()

So ive seen people say to do this os.startfile(path[, operation][, arguments][, cwd][, show_cmd]) but the problem with that is that I have no idea what i have to put in operation, arguments, cwd and show cwd. Im sorry if this is a simple solution, but im new to Python and still learning it.

I appreciate every help :)

CodePudding user response:

from the doc: "When another operation is given, it must be a “command verb” that specifies what should be done with the file. Common verbs documented by Microsoft are 'print' and 'edit' (to be used on files) as well as 'explore' and 'find' (to be used on directories).". For your purpose you can leave it empty.

However, I'd recommend you not to use os.startfile. I guess you're program is in another file, for exemple "play_game.py", with a function called "start_game()" If it is in your current directory, then you can import it like so:

import os
import random
import play_game

main = tk.Tk()
main.title("Minigames")
main.geometry("450x400")
main.resizable(width= False, height= False)

def forwardtoo():
    play_game.start_game()
    main.destroy()




b1 = tk.Button(main, text="Number Guesser", command=forwardtoo)
b1.place(x = 170, y = 250)

main.mainloop()

Every function in the file can now be called with play_game.NAME_OF_THE_FUNCTION() You should also create an input textfields so that the user can play with the inputs he wants, you'd need to store the inputs and then create a button linked to start_game() which would retrieve the user's input. Hope this was clear and it helps!

CodePudding user response:

If I understand correctly, based on the button clicked You want to run specific game? If so, the best way to do this is to import some function responsible for running the game and then run in instead of executing the entire script. So for example:

Let's assume that file number_guesser.py looks like this:

def guesser_logic():
    # some_logic
    ...

def run():
    guesser_logic()

Then in the file containing button actions (let's name it main.py) You could do:

import os
import random

from number_guesser import run

main = tk.Tk()
main.title("Minigames")
main.geometry("450x400")
main.resizable(width= False, height= False)

def forwardtoo():
    run()
    main.destroy()

b1 = tk.Button(main, text="Number Guesser", command=forwardtoo)
b1.place(x = 170, y = 250)

main.mainloop()

Notice the extra import and the change I made.

  • Related