I would to start a programm when I press a button. I import my programm and now I don‘t know how to start the programm (with classes in it). How can I start it?
Forum Archive
Start a Skript which is imported
Which menu button?
@mikael a button which is in the menu. So I click the button and want to start the game. I import the game and now....? I don‘t know how to import all the classes.
@Python567 so you want to import and start a .py?
Édit: perhaps, could you use runpy
@cvp yes! Do you know how?
@cvp, I am pretty sure that the answer is easier than using runpy, which I have never needed in ”real life”.
@Python567, it is still not clear which ”menu” you are referring to. Have you tried the triangle-shaped ”run” or ”play” button? Are you familiar with the basic UI of Pythonista, as described in the manual here?
I create a menu with a play button. If I press this button, I would that the head game started. I don‘t want to write everything in the skript again where the menu is. so I import the game in the menu. But I don‘t know how to start the game which was imported! Is that understandable?
@Python567 easiest way is that, in your little script where you have your menu, you do
import big_script
big_script.main()
That's all folks
@Python567 If you are trying run a python file from an existing ui app, this may help.
import sys
import ui
import runpy
def runpy_game(sender):
runpy.run_path('mygame.py', run_name='__main__')
def run_game(sender):
import mygame
mygame.main()
v = ui.View()
v.name = 'Game Launcher'
v.background_color='white'
v.right_button_items = [ui.ButtonItem(image=ui.Image.named('iob:ios7_play_24'), action=runpy_game)]
btnplay = ui.Button(image=ui.Image.named('iob:ios7_play_24'))
btnplay.action = run_game
v.add_subview(btnplay)
v.present()
@bosco I think we cross our posts, with the same idea
@cvp yep :-)
@cvp I import my Game and there where I define the menu button, I write Game.main() is that right? Because I get a error: Module Game has no attribute main.
@Python567 it is usual to write a script as
import ...
class ...
def main():
...
...
...
if __name__ == '__main__':
main()
@cvp here is my button menu skript:
```
import Tron
from scene import *
import ui
import sound
A = Action
class ButtonNode (SpriteNode):
def init(self, title, args, kwargs):
SpriteNode.init(self, 'pzl:Button1', args, **kwargs)
button_font = ('Avenir Next', 20)
self.title_label = LabelNode(title, font=button_font, color='black', position=(0, 1), parent=self)
self.title = title
class MenuScene (Scene):
def init(self, title, subtitle, button_titles, args, kwargs):
Scene.init(self, args, **kwargs)
self.title = 'title'#title
self.subtitle = 'subtitle'#subtitle
self.button_titles = ['Play']#button_titles
def setup(self):
button_font = ('Avenir Next', 20)
title_font = ('Avenir Next', 36)
num_buttons = len(self.button_titles)
self.bg = SpriteNode(color='black', parent=self)
bg_shape = ui.Path.rounded_rect(0, 0, 240, num_buttons * 64 + 140, 8)
bg_shape.line_width = 4
shadow = ((0, 0, 0, 0.35), 0, 0, 24)
self.menu_bg = ShapeNode(bg_shape, (1,1,1,0.9), '#15a4ff', shadow=shadow, parent=self)
self.title_label = LabelNode(self.title, font=title_font, color='black', position=(0, self.menu_bg.size.h/2 - 40), parent=self.menu_bg)
self.title_label.anchor_point = (0.5, 1)
self.subtitle_label = LabelNode(self.subtitle, font=button_font, position=(0, self.menu_bg.size.h/2 - 100), color='black', parent=self.menu_bg)
self.subtitle_label.anchor_point = (0.5, 1)
self.buttons = []
for i, title in enumerate(reversed(self.button_titles)):
btn = ButtonNode(title, parent=self.menu_bg)
btn.position = 0, i * 64 - (num_buttons-1) * 32 - 50
self.buttons.append(btn)
self.did_change_size()
self.menu_bg.scale = 0
self.bg.alpha = 0
self.bg.run_action(A.fade_to(0.4))
self.menu_bg.run_action(A.scale_to(1, 0.3, TIMING_EASE_OUT_2))
self.background_color = 'white'
def did_change_size(self):
self.bg.size = self.size + (2, 2)
self.bg.position = self.size/2
self.menu_bg.position = self.size/2
def touch_began(self, touch):
touch_loc = self.menu_bg.point_from_scene(touch.location)
for btn in self.buttons:
if touch_loc in btn.frame:
sound.play_effect('8ve:8ve-beep-hightone')
btn.texture = Texture('pzl:Button2')
self.button_tapped(btn)
def touch_ended(self, touch):
for btn in self.buttons:
btn.texture = Texture('pzl:Button1')
if self.presenting_scene and touch_loc in btn.frame:
new_title = self.presenting_scene.menu_button_selected(btn.title)
if new_title:
btn.title = new_title
btn.title_label.text = new_title
def menu(self):
pass
def button_tapped(self, sender):
print('button tapped:', sender.title)
Tron.main()
if name == 'main':
run(MenuScene('LeiRacers', 'Subtitle', ['Play']))
Atdef button_tappedI writeTron.main()``` but there come an error!
@Python567 you did not import Tron...
@cvp I import Tron, but I don‘t copy it here, my mystake. But the error is not away
@Python567 and your Tron module does have a def main():
@cvp no :/
@Python567 thus you find the reason 😀