在Tkinter的Canvas上举起Canvas [英] Lift and raise a Canvas over a Canvas in tkinter

查看:76
本文介绍了在Tkinter的Canvas上举起Canvas的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建游戏,并且正在使用tkinter来构建GUI。

I'm creating a game, and I am using tkinter to build the GUI.

在此游戏中,我希望用户从一个以Canvas填充的窗口开始,以图像为背景,并在某些项目窗口中添加一些按钮。这个画布是游戏的主画布。

In this game, I want the user to start with a window, filled by a Canvas, with an image as background, and some buttons in some sort of item windows. And this Canvas is the home Canvas of the game.

问题是我希望用户单击按钮以登陆其他Canvas,后者将托管其他内容,例如地图或其他按钮,以供其他操作使用。实际上,我想叠加几个画布(如Z-index方法一样),并在需要时将画布放在列表的顶部(例如,如果单击一个按钮)。

The problem is that I want the user to click on a Button to land on other Canvas, who will host other things like the map or others buttons for others actions. Actually, I would like to superimpose several canvas (as in the Z-index method), and put a canvas on the top of the list, when I want it (if I click on a button for example).

我已经搜索过,不得不改变主意几次,现在我真的不知道该怎么做。

I already searched and I had to change my mind several times, and now I really don't know how to do it.

我在Stack Overflow上找到了以下代码,但是它是为Python 2编码的(我认为),并且我开始在Python 3中进行编码,因此无法将其翻译为Python 3并解决我的问题。 / p>

I find the following code here on Stack Overflow, but it is coded for Python 2 (I think), and I'm starting coding in Python 3, so I am not able to translate it to Python 3 and solve my problem.

import Tkinter as tk

class SampleApp(tk.Tk):

   def __init__(self, *args, **kwargs):
       tk.Tk.__init__(self, *args, **kwargs)
       self.frame = tk.Frame(self)
       self.frame.pack(side="top", fill="both", expand=True)
       self.label = tk.Label(self, text="Hello, world")
       button1 = tk.Button(self, text="Click to hide label",
                           command=self.hide_label)
       button2 = tk.Button(self, text="Click to show label",
                           command=self.show_label)
       self.label.pack(in_=self.frame)
       button1.pack(in_=self.frame)
       button2.pack(in_=self.frame)

    def show_label(self, event=None):
        self.label.lift(self.frame)

    def hide_label(self, event=None):
        self.label.lower(self.frame)


if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

使用网格的代码:

from tkinter import *

fenetre = Tk()
fenetre.title(my game)

# acceuil
# variable acceuil
largeur = 700
hauteur = 430

BG_acceuil = PhotoImage(file="BG_acceuil.gif")
acceuil = Canvas(fenetre, width=largeur, height=hauteur)
acceuil.create_image(0, 0, anchor=NW, image=BG_acceuil)
acceuil.grid(row=0)
acceuil.pack()

# fond
fond = PhotoImage(file="BG_acceuil.gif")
acceuil2 = Canvas(fenetre, width=largeur, height=hauteur)
acceuil2.create_image(0, 0, anchor=NW, image=fond)
acceuil2.pack()

# variable bt_jouer
x0 = 80
y0 = 230


class hide_me():

    def hide_me(event, widget, pos):
        widget.grid_forget()

    def show_me(event, widget, pos):
        widget.grid(row=pos)


# Boutton jouer
BT_jouer = Button(acceuil, text="Jouer", command=hide_me())
BT_jouer.configure(width=10, activebackground="#33B5E5", relief=GROOVE)
BT_jouer_window = acceuil.create_window(x0, y0, window=BT_jouer,)
BT_jouer.bind('<Button-1>', lambda event: hide_me(event, BT_jouer, 1))
BT_jouer.grid(row=1)

# Bouton règle
BT_regle = Button(acceuil2, text="Règles", command=fenetre.destroy)
BT_regle.configure(width=10, activebackground="#33B5E5", relief=FLAT,    bd=0)
BT_regle_window = acceuil2.create_window(x0, y0 + 50, window=BT_regle)

# Boutton quitter
BT_quit = Button(acceuil, text="Quitter", command=fenetre.destroy)
BT_quit.configure(width=10, activebackground="#33B5E5", relief=FLAT)
BT_quit_window = acceuil.create_window(x0, y0 + 100, window=BT_quit)

fenetre.mainloop()


推荐答案

答案很简单:转换到Python3,将Tkinter更改为tkinter,就可以了!

The answer is very easy: To convert to Python3, change Tkinter to tkinter, and it works!

import tkinter as tk

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
       tk.Tk.__init__(self, *args, **kwargs)
       self.frame = tk.Frame(self)
       self.frame.pack(side="top", fill="both", expand=True)
       self.label = tk.Label(self, text="Hello, world")
       button1 = tk.Button(self, text="Click to hide label",
                           command=self.hide_label)
       button2 = tk.Button(self, text="Click to show label",
                           command=self.show_label)
       self.label.pack(in_=self.frame)
       button1.pack(in_=self.frame)
       button2.pack(in_=self.frame)

    def show_label(self, event=None):
        self.label.lift(self.frame)

    def hide_label(self, event=None):
        self.label.lower(self.frame)

def main():
    app = SampleApp()
    app.mainloop()  
    return 0

if __name__ == '__main__':
    main()

注意:您并没有真正隐藏标签-它仍然占用画布上的空间。 以下代码,从此条目中,实际上删除了该项目。然后可以使用pack()调用来调用它:

Note: You are not really hiding the label - it still occupies space on the canvas. The following code, from this entry, really removes the item. It can then be recalled with a pack() call:

from Tkinter import *

def hide_me(event):
    event.widget.pack_forget()

root = Tk()
btn=Button(root, text="Click")
btn.bind('<Button-1>', hide_me)
btn.pack()
btn2=Button(root, text="Click too")
btn2.bind('<Button-1>', hide_me)
btn2.pack()
root.mainloop()

我做了一些测试,并制作了一个与您相同的程序...唯一的问题是,未隐藏的小部件始终在末尾打包:

I did some testing, and made an equivalent program to yours... The only problem is that the unhidden widget is always packed at the end:

from tkinter import *

def hide_me(event, widget):
    widget.pack_forget()

def show_me(event, widget):
    widget.pack()

root = Tk()
lbl = Label(root, text="Victim")

btn = Button(root, text="Hide the victim")
btn.bind('<Button-1>', lambda event: hide_me(event, lbl))
btn.pack()

btn2 = Button(root, text="Show the victim")
btn2.bind('<Button-1>', lambda event: show_me(event, lbl))
btn2.pack()

lbl.pack()

root.mainloop()

更好的版本使用网格()封隔器。您实际上可以在此处将已忘记小部件恢复到其原始位置。只是稍微复杂一点:)

A better version uses the grid() packer. Here you can actually restore the 'forgotten' widget to its original position. Only slightly more complicated :)

from tkinter import *

def hide_me(event, widget, pos):
    widget.grid_forget()

def show_me(event, widget, pos):
    widget.grid(row = pos)

root = Tk()
lbl = Label(root, text="Victim")
lbl.grid(row = 0)

btn = Button(root, text="Hide the victim")
btn.bind('<Button-1>', lambda event: hide_me(event, lbl, 0))
btn.grid(row = 1)

btn2 = Button(root, text="Show the victim")
btn2.bind('<Button-1>', lambda event: show_me(event, lbl, 0))
btn2.grid(row = 2)


root.mainloop()

编辑:评论中的另一项观察:布莱恩·奥克利(Bryan Oakley)指出,如果使用 .grid_remove()而不是 .grid_forget(),那么坐标将不会丢失,并且简单的 .grid()将在其位置恢复小部件。

Another observation from the comments: Bryan Oakley commented that if you use .grid_remove() instead of .grid_forget(), then the coordinates will not be lost, and a simple .grid() will restore the widget at its location.

这篇关于在Tkinter的Canvas上举起Canvas的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆