Python Forum
Running another python file from code
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Running another python file from code
#1
import turtle as tl
import os

home = tl.Screen()
home.bgcolor("black")
home.title("Home Screen")

lastlevel = 1
start = tl.Turtle()
start.color("green")
start.shape("square")

def startlevel(x,y):
    global lastlevel
    s = "Level %s.py" % lastlevel
    os.system(s)
    return
    
start.onclick(startlevel)

tl.mainloop()
This is my code, the other python file (Level 1.py) works perfectly fine. When i press on the turtle, a screen flickers on for just a moment and than disappears. Can you help me make this work?
Reply
#2
First of all, you shouldn't use global. You should pass that as a parameter. Of course onclick requires a function with two parameters. So I would either do this as a class, or find another way to start your game.

Second, that's not how you run python code. You should make level 1.py a file that can be imported (really you just need to change the name). It should have a function that can be run, or a class that can be instantiated.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in running a code akbarza 7 626 Feb-14-2024, 02:57 PM
Last Post: snippsat
  writing and running code in vscode without saving it akbarza 1 379 Jan-11-2024, 02:59 PM
Last Post: deanhystad
  the order of running code in a decorator function akbarza 2 518 Nov-10-2023, 08:09 AM
Last Post: akbarza
  error "cannot identify image file" part way through running hatflyer 0 663 Nov-02-2023, 11:45 PM
Last Post: hatflyer
  Web project and running a .py file emont 0 636 Dec-11-2022, 11:15 PM
Last Post: emont
  Code running many times nad not just one? korenron 4 1,356 Jul-24-2022, 08:12 AM
Last Post: korenron
  Error while running code on VSC maiya 4 3,713 Jul-01-2022, 02:51 PM
Last Post: maiya
  Problem with importing Python file in Visual Studio Code DXav 7 5,060 Jun-15-2022, 12:54 PM
Last Post: snippsat
  code running for more than an hour now, yet didn't get any result, what should I do? aiden 2 1,492 Apr-06-2022, 03:41 PM
Last Post: Gribouillis
  Python code to read second line from CSV files and create a master CSV file sh1704 1 2,394 Feb-13-2022, 07:13 PM
Last Post: menator01

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020