2017-07-16 2 views
0

Zelleのグラフパッケージを使用してゲーム "dice poker"を作成し、メインスクリーンにテキストファイルを開くボタンがあります。ボタンをクリックするとテキストファイルが開きますが、メインウィンドウは閉じます。親ウィンドウを開いたままにするにはどうすればよいですか?親ウィンドウを閉じずにグラフィックスボタンから実行可能ファイルを開く

ボタンクラスは以下の通りです:

from graphics import * 
from tkinter import Button as tkButton 

class Button(): 

    """A button is a labeled rectangle in a window. 
    It is activated or deactivated with the activate() 
    and deactivate() methods. The clicked(p) method 
    returns true if the button is active and p is inside it.""" 

    def __init__(self, win, center, width, height, label): 
     """ Creates a rectangular button, eg: 
     qb = Button(myWin, centerPoint, width, height, 'Quit') """ 

     w,h = width/2.0, height/2.0 
     x,y = center.getX(), center.getY() 
     self.xmax, self.xmin = x+w, x-w 
     self.ymax, self.ymin = y+h, y-h 
     p1 = Point(self.xmin, self.ymin) 
     p2 = Point(self.xmax, self.ymax) 
     self.rect = Rectangle(p1,p2) 
     self.rect.setFill('lightgray') 
     self.rect.draw(win) 
     self.label = Text(center, label) 
     self.label.draw(win) 
     self.deactivate() 

    def clicked(self, p): 
     "Returns true if button active and p is inside" 
     return (self.active and 
       self.xmin <= p.getX() <= self.xmax and 
       self.ymin <= p.getY() <= self.ymax) 

    def getLabel(self): 
     "Returns the label string of this button." 
     return self.label.getText() 

    def activate(self): 
     "Sets this button to 'active'." 
     self.label.setFill('black') 
     self.rect.setWidth(2) 
     self.active = True 

    def deactivate(self): 
     "Sets this button to 'inactive'." 
     self.label.setFill('darkgrey') 
     self.rect.setWidth(1) 
     self.active = False 

どのように私はこのTkinterの実装と同様の方法で実行可能ファイルを開くことができますcommand引数含めることができますコマンドをすることができ

import Tkinter as tk 

def create_window(): 
    window = tk.Toplevel(root) 

root = tk.Tk() 
b = tk.Button(root, text="Create new window", command=create_window) 
b.pack() 

root.mainloop() 

subprocess.run(['open', '-t', 'poker_help.txt'])まだ元のウィンドウを開いたままにしますか?

Zelleグラフィックス、またTkinterの上に構築されているTkinterのとカメとは異なり、doesnの」:

答えて

1

は、私はあなたが(Macをご例えば)トップレベルのコードが含まれていなかったので、いくつかの仮定をしなければなりません明示的なwin.mainloop()呼び出しを持って、Tkイベントハンドラに制御を渡して、待っているイベントが発生するのを待ちます。代わりに、あなたがあなたのボタンをオフに発射するマウスクリックを取得するそう一度、一緒に自分自身を1にパッチを適用する必要があり、プログラムがファイルの終わりを通って落下し、メインウィンドウが閉じます。from button import Buttonをもたらします

import subprocess 
from graphics import * 
from button import Button 

win = GraphWin() 

help_button = Button(win, Point(150, 150), 50, 50, "Help") 
help_button.activate() 

quit_button = Button(win, Point(50, 50), 50, 50, "Quit") 
quit_button.activate() 

while True: 
    point = win.getMouse() 

    if help_button.clicked(point): 
     subprocess.call(['open', '-t', 'poker_help.txt']) 
    elif quit_button.clicked(point): 
     win.close() 

をご上記のボタンコード。確認するもう一つのことは、あなたのウィンドウは実際に閉じているだけで、それを上に開いた新しいウィンドウではあいまいではありません。

関連する問題