我想创建一个可视化显示,它由我在主脚本中不同时刻调用的不同画布组成。而且他们都有特定的持续时间。我希望画布在我的窗口中居中,窗口可以调整大小。此外,我想在连接到我的电脑的第二个屏幕上显示此窗口。 到目前为止,我成功创建了窗口和画布。我在stackoverflow中的另一篇文章中重用了ebarr发布的ResizingCanvas(Canvas)类。但是:帆布和窗户没有居中;当我最大化窗口时:画布不会跟随;当我想要关闭窗口时:关闭按钮不起作用。在十字架上,我试图在画布中间创建一个白色十字架。 如果您对如何在另一个屏幕上显示我的窗口有任何想法,那将是非常棒的。提前感谢您的时间和答案!
import tkinter
from tkinter import font
import time
class ResizingCanvas(tkinter.Canvas):
def __init__(self,parent,**kwargs):
tkinter.Canvas.__init__(self,parent,**kwargs)
self.bind("<Configure>", self.on_resize)
self.height = self.winfo_reqheight()
self.width = self.winfo_reqwidth()
def on_resize(self,event):
# determine the ratio of old width/height to new width/height
wscale = float(event.width)/self.width
hscale = float(event.height)/self.height
self.width = event.width
self.height = event.height
# resize the canvas
self.config(width=self.width, height=self.height)
# rescale all the objects tagged with the "all" tag
self.scale("all",0,0,wscale,hscale)
class display():
def __init__(self, Reponse=None,):
self.root = tkinter.Tk() # Création de la fenêtre racine
self.root.title('Feedback') # Ajout d'un titre
self.myframe = tkinter.Frame(self.root)
self.myframe.pack(fill=tkinter.BOTH, expand=tkinter.YES)
self.mycanvas = ResizingCanvas(self.myframe,width=850, height=400, bg="black", highlightthickness=0)
# tag all of the drawn widgets
self.mycanvas.addtag_all("all")
self.helv36 = font.Font(self.root,family="Helvetica",size=24, weight="bold")
font.families()
def test(self):
self.mycanvas.create_text(100,100,text=" Bonne réponse ", fill="white")
self.mycanvas.pack(fill=tkinter.BOTH, expand=tkinter.YES)
self.root.update()
time.sleep(5)
self.clear()
def returnReponse(self,Reponse):
if(Reponse==True):
self.mycanvas.create_text(300, 300, text=" Bonne réponse ", fill="white",font=self.helv36)
else:
self.mycanvas.create_text(300, 300, text=" Mauvaise réponse ", fill="white",font=self.helv36)
self.mycanvas.pack(fill=tkinter.BOTH, expand=tkinter.YES)
self.root.update()
time.sleep(1.250)
self.clear()
def cross(self):
self.mycanvas.create_rectangle((-150,-50),(150,50), fill='white',outline ='white', width=2)
self.mycanvas.create_rectangle((50,150),(50,150), fill='white',outline='white', width=2)
self.mycanvas.pack(fill=tkinter.BOTH, expand=tkinter.YES)
self.root.update()
time.sleep(0.250)
self.clear()
def clear(self):
self.canvas.delete('all')
self.root.update()
fenetre=display()
fenetre.test()
fenetre.cross()
fenetre.returnReponse(True)