如何使用Tkinter阻止画布上的所有内容向上移动

时间:2019-04-29 16:05:41

标签: python python-3.x tkinter tkinter-canvas

我正在使用tkinter制作一个简单的数学游戏,但屏幕上的所有元素都向上移动时遇到了一些问题。

所以我有一些代码用画布创建屏幕A。背景图像以及一些按钮和文本。当按下一个按钮时,屏幕被改变,另一个按钮被按下,它通过清除画布并重新绘制每个元素而返回到屏幕A。我尝试过更改锚点,但似乎没有什么作用。

按下返回按钮后,画布看起来像这样

The canvas looks like this after the return button is pressed

第一次访问屏幕时,它看起来应该是正确的。

谢谢:)任何帮助将不胜感激

def showendscreen(self,returned):
    self.clearcanvas()

    if returned==False:
        self.newhighscore=checkifhighscore(self.mode)
        self.pictures=Pictures()
        if self.newhighscore == True:
            self.endtext="          congratulations \n you've got a new highscore!!"
            self.photo=self.pictures.congrats
        elif self.newhighscore==False:
            self.endtext = "Unlucky, keep practicing!"
        elif self.newhighscore == "same":
            self.photo=self.pictures.happy
            self.endtext="well done, you got your personal best again!"

    self.image1=Image.open(self.photo)
    (self.w,self.h)=self.image1.size
    self.image1a=ImageTk.PhotoImage(self.image1)
    self.item1=self.canvas.create_image(self.w/2,self.h/2, image=self.image1a, anchor="center")
    self.canvas.create_text(self.w/2,50,fill="purple",font="Purisa 40 bold",anchor='center',text=self.endtext)
    self.canvas.create_text(self.w/2,600,fill="purple",font="Purisa 40 bold",anchor='n',text="You scored: "+ str(self.countq))
    self.canvas.grid()
    self.image1.close()
    self.btn3=tk.Button(root, width=18, height =1,text="Questions Answered", font = "Purisa 20",command=self.questionsanswered)
    self.btn3_window = self.canvas.create_window(((self.w/2)-150),(self.h/2)+3 ,anchor="center", window=self.btn3)    
    self.btn4=tk.Button(root, width=18, height =1,text="Scoreboard",font = "Purisa 20",command=self.scoreboard)
    self.btn4_window = self.canvas.create_window(((self.w/2)+150),(self.h/2)+3 ,anchor="center", window=self.btn4)
    self.btn5=tk.Button(root, width=18, height =1,text="My Progress",font = "Purisa 20", command = self.progress)
    self.btn5_window = self.canvas.create_window(((self.w/2)),(self.h/2)+60 ,anchor="center", window=self.btn5)
    self.btn6=tk.Button(root, width=10, height=1, text="Restart", font ="Purisa 15", command = restartgame)
    self.btn6_window=self.canvas.create_window(((self.w/2)),(self.h/2)+120 ,anchor="center", window=self.btn6)
    self.canvas.grid()
def clearcanvas(self): self.canvas.delete('all')

在功能上方重新运行的按钮:

self.backbutton=tk.Button(root,image=self.returnb, command= lambda: self.showendscreen(True), anchor="nw") 
self.backbutton_window = self.canvas.create_window(10,60,anchor="nw", window=self.backbutton)

0 个答案:

没有答案