2017-07-16 54 views
0

我使用Zelle的圖形包創建了一個遊戲「骰子撲克」,並在打開文本文件的主屏幕上有一個按鈕。文本文件在按鈕被點擊時打開,但主窗口關閉。我怎樣才能保持父窗口打開?從關閉父窗口的圖形按鈕打開可執行文件

按鈕類如下:

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 

我怎麼能包括command論點,即可以打開的方式與此類似Tkinter的執行的可執行文件:

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'])仍然保持原來的窗口打開?

回答

1

我必須作出一些假設,因爲你不包括頂級代碼(例如,您使用的是Mac):

Zelle圖形,不同的Tkinter和烏龜,這也是建立在Tkinter的,沒有按」沒有明確的調用將控制權交給Tk事件處理程序,以等待事件發生。相反,你需要修補一個一起自己,否則一旦你的鼠標點擊即觸發了你的按鈕,程序下落通過文件的末尾,主窗口關閉:

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() 

from button import Button帶給你上面的按鈕代碼。另一件要檢查的是你的窗口實際上是關閉的,而不是簡單地被在它上面打開的新窗口遮擋。

相關問題