2017-10-16 72 views
-1

私はpygameでpythonを使って小さなゲームを作成しています。Python - 別のファイルのtkinterウィンドウでファイルを実行するには

少し問題があります: 私はpythonファイルとゲームの別のファイルに私のinterfarceを持っています。 問題は、私のインターフェイスで再生をクリックすると、セカンドウインドウでゲームファイルをロードし、このファイルをインターファースの現在のウィンドウで実行したいということです。 お時間をありがとう!

興味深い部分コード:

インタフェースファイルに(ソロボタンをクリックすると、ソロの定義を実行)

def solo(): 
import Solo 
fenetre.mainloop() 

solo=Button(fenetre, image = pho15, width = 280, height = 81, borderwidth = 0, cursor = "hand2", command = solo) 

ソロファイル(新しいウィンドウを生成する)

fen=Tk() 
fen.title("Super Crash") 

coords = (175, 345) 
image = Image.open("on.png") 
photo = ImageTk.PhotoImage(image) 
score=0 

pygame.mixer.init() 
crash = pygame.mixer.Sound("crash.ogg") 
crash.set_volume(0.1) 
bruit = pygame.mixer.Sound("bip.ogg") 
bruit.set_volume(0.01) 

ima1 = Image.open("Rejouer.jpg") 
pho1 = ImageTk.PhotoImage(ima1) 
ima2 = Image.open("Menu.jpg") 
pho2 = ImageTk.PhotoImage(ima2) 

can=Canvas(fen,bg="white", height=800, width=1000) 
can.create_image(0,0, anchor = NW, image = photo) 
can.focus_set() 
can.bind("<KeyPress>", monter) 
can.pack() 

anim() 

init() 
+0

[Tkinterの又はwxPythonのフレームにpygameのウィンドウを埋め込む](https://stackoverflow.com/questions/23319059/embedding-a-pygame-window-into-a-tkinter-orの可能な重複-wxpython-frame) –

答えて

0

fen = Tk()は新しいアプリケーションウィンドウを作成するため、別のウィンドウを作成する理由です。あなたが検索している場合は、これを行う例がいくつかありますが、別の例では外部のtkinterアプリケーションを起動する方法があります。

メインファイル実行このファイル(main.pyまたは何でも)。これは、外部(solo.py)ファイルでボタンを

from tkinter import * 
from subprocess import Popen 

def launchExternal(parent): 

    container = Frame(parent, container=True) 
    container.pack(expand=True, fill='both') 
    ''' Pass the window id of the container frame in this main application 
     to the external program. See solo.py for putting content in the 
     container. 
    ''' 
    process = Popen(['python', 'solo.py', str(container.winfo_id())]) 

# Create the main application GUI (this file) 
root = Tk() 
# Define the external program name (will be solo.py) 
program = 'solo' 
# For fun, click this button multiple times to see what happens. 
solo = Button(root, text="Launch solo", width = 10, height = 10, borderwidth = 0, 
    cursor = "hand2", command = lambda r=root: launchExternal(r)) 
solo.pack() 

root.mainloop() 

をクリックします。

from tkinter import * 
import sys 

def createInnerUI(master): 
    canvas = Canvas(master, bg='blue') 
    canvas.pack(pady=5) 
    label = Label(master, text="Demonstrate embedded application", bg='green') 
    label.pack() 

if len(sys.argv) < 1: 
    root=Tk() 
else: 
    target_window_id = sys.argv[-1] 
    root = Tk(use=target_window_id) 

createInnerUI(root) 
root.mainloop() 
+0

ありがとう、完全なsolo.pyはcreateInnerUI関数にありますか?プログラム全体では機能しません。 –

+0

必ずしもそうではありません。他の機能を作成して、必要に応じてGUIに要素を追加することができます。例えば、キャンバスは 'root = Tk(use = target_window_id)'の直後、 'createInnerUI(root)'の呼び出しの前に作成されていましたが、ラベル付きの関数に配置することを選択しました。見て回るように物を動かしてみてください。 –

+0

*プログラム全体では機能しません。*あなたが何を意味するのか分かりません。 –

関連する問題