从图形按钮打开可执行文件而不关闭父级 window
Open executable file from graphics button without closing parent window
我使用 Zelle 的图形包创建了一个游戏 "dice poker",并且在主屏幕上有一个按钮可以打开一个文本文件。单击按钮时文本文件打开,但主 window 关闭。我怎样才能让父级 window 保持打开状态?
按钮class在下方:
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'])
并且仍然保持原来的window打开?
我必须做出一些假设,因为您没有包含顶级代码(例如,您在 Mac 上):
Zelle 图形与同样基于 tkinter 构建的 tkinter 和 turtle 不同,它没有明确的 win.mainloop()
调用来将控制权移交给 Tk 事件处理程序以空闲等待事件发生。相反,您必须自己将一个拼凑在一起,否则一旦您点击鼠标触发了您的按钮,程序就会从文件末尾掉落并且主要 window 关闭:
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
引入您上面的按钮代码。另一件要检查的事情是你的 window 实际上正在关闭,而不是简单地被上面打开的新 window 遮挡。
我使用 Zelle 的图形包创建了一个游戏 "dice poker",并且在主屏幕上有一个按钮可以打开一个文本文件。单击按钮时文本文件打开,但主 window 关闭。我怎样才能让父级 window 保持打开状态?
按钮class在下方:
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'])
并且仍然保持原来的window打开?
我必须做出一些假设,因为您没有包含顶级代码(例如,您在 Mac 上):
Zelle 图形与同样基于 tkinter 构建的 tkinter 和 turtle 不同,它没有明确的 win.mainloop()
调用来将控制权移交给 Tk 事件处理程序以空闲等待事件发生。相反,您必须自己将一个拼凑在一起,否则一旦您点击鼠标触发了您的按钮,程序就会从文件末尾掉落并且主要 window 关闭:
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
引入您上面的按钮代码。另一件要检查的事情是你的 window 实际上正在关闭,而不是简单地被上面打开的新 window 遮挡。