Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用tkinter隐藏和显示按钮?_Python_Tkinter - Fatal编程技术网

Python 如何使用tkinter隐藏和显示按钮?

Python 如何使用tkinter隐藏和显示按钮?,python,tkinter,Python,Tkinter,我一直在创造一个口袋妖怪匹配游戏,但我不知道如何隐藏cauculate按钮如果有人知道答案,请告诉我。我尝试了一些网站,但没有成功。你们能帮帮我吗??我试着忘记包和其他东西。还有,如果有任何错误告诉我。我正在使用VisualStudio代码来编写代码 这是我的密码: #!/usr/bin/env python # -*- coding: utf-8 -*- import pygame import time from PIL import Image, ImageTk import random

我一直在创造一个口袋妖怪匹配游戏,但我不知道如何隐藏cauculate按钮如果有人知道答案,请告诉我。我尝试了一些网站,但没有成功。你们能帮帮我吗??我试着忘记包和其他东西。还有,如果有任何错误告诉我。我正在使用VisualStudio代码来编写代码

这是我的密码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pygame
import time
from PIL import Image, ImageTk
import random
from pygame import mixer

from tkinter import *

try:
    # Tkinter for Python 2.xx
    import Tkinter as tk
except ImportError:
    # Tkinter for Python 3.xx
    import tkinter as tk
APP_TITLE = "Pokemon Game"
APP_XPOS = 100
APP_YPOS = 100
APP_WIDTH = 300
APP_HEIGHT = 200
IMAGE_PATH = "images/"
mixer.init()

class CreateCanvasObject(object):
    def __init__(self, canvas, image_name, xpos, ypos):
        self.canvas = canvas
        self.image_name = image_name
        self.xpos, self.ypos = xpos, ypos

        #image = Image.open("C:\\Users\\happy\\Desktop\\test.jpg")
        #photo = ImageTk.PhotoImage(image)

        self.tk_image = ImageTk.PhotoImage(
            file="{}{}".format(IMAGE_PATH, image_name))
        self.image_obj= canvas.create_image(
            xpos, ypos, image=self.tk_image)



#        self.tk_image = tk.PhotoImage(
#            file="{}{}".format(IMAGE_PATH, image_name))
#        self.image_obj= canvas.create_image(
#            xpos, ypos, image=self.tk_image)
        canvas.tag_bind(self.image_obj, '<Button1-Motion>', self.move)
        canvas.tag_bind(self.image_obj, '<ButtonRelease-1>', self.release)
        self.move_flag = False
        #self.text_id = self.canvas.create_text(950,50, font='Times 24',text="                                                                 ")
    def move(self, event):
        #self.image_obj[0]:
        #print(self.image_1.x)
        if self.move_flag:
            new_xpos, new_ypos = event.x, event.y
            self.canvas.move(self.image_obj,
                new_xpos-self.mouse_xpos ,new_ypos-self.mouse_ypos)
            self.mouse_xpos = new_xpos
            self.mouse_ypos = new_ypos
        else:
            self.move_flag = True
            self.canvas.tag_raise(self.image_obj)
            self.mouse_xpos = event.x
            self.mouse_ypos = event.y
    def release(self, event):
        self.move_flag = False
class Application(tk.Frame):
    def __init__(self, master):
        #print(self)
        self.master = master
        self.master.protocol("WM_DELETE_WINDOW", self.close)
        tk.Frame.__init__(self, master)
        self.canvas = tk.Canvas(self, width=400, height=400, bg='#AFFAAF',
            highlightthickness=0)
        self.canvas.pack(fill="both", expand=True)
        pokemons = ["Charizard", "Charmeleon", 'Charmander', 'Blastoise', 'Wartortle','Squirtle','Venusaur','Ivysaur','Bulbsaur']
        random.shuffle(pokemons)
        for i in range(0,9):
            self.image_1 = CreateCanvasObject(self.canvas,pokemons[i]+'.png', 100*i+350, 500)
            #self.image_2 =CreateCanvasObject(self.canvas, "Charmeleon.png", 200, 500)
            #self.image_3 = CreateCanvasObject(self.canvas, "Charmander.png", 300, 500)
            #self.image_4 =CreateCanvasObject(self.canvas, "Blastoise.png", 400, 500)
            #self.image_5 = CreateCanvasObject(self.canvas, "Wartortle.png", 500, 500)
            #self.image_6 =CreateCanvasObject(self.canvas, "Squirtle.png", 600, 500)
            #self.image_7 =CreateCanvasObject(self.canvas, "Venusaur.png", 700, 500)
            #self.image_8 = CreateCanvasObject(self.canvas, "Ivysaur.png", 800, 500)
            #self.image_9 =CreateCanvasObject(self.canvas, "Bulbsaur.png", 900, 500)
        self.canvas.create_rectangle(350, 50, 600, 300) # fill='white')
        self.canvas.create_rectangle(650, 50, 900, 300) #, fill='white')
        self.canvas.create_rectangle(950, 50, 1200, 300) #, fill='white')
        self.canvas.create_rectangle(320, 550, 1250, 450)
        self.text_id1 = self.canvas.create_text(500,750, font='Times 24',text="")
        self.text_id = self.canvas.create_text(650,750, font='Times 24',text="")
        self.text_id2 = self.canvas.create_text(800,750, font='Times 24',text="")
        self.text_id3 = self.canvas.create_text(1000,750, font='Times 24',text="")
        self.text_id4 = self.canvas.create_text(800,400, font='Times 24',text="Drag the pokemons in their right evolution place. When you finished, press [Calculate] ")
        set1={pokemons.index('Charizard')+1,pokemons.index('Charmeleon')+1,pokemons.index('Charmander')+1}
        set2={pokemons.index('Blastoise')+1,pokemons.index('Wartortle')+1,pokemons.index('Squirtle')+1}
        set3={pokemons.index('Venusaur')+1,pokemons.index('Ivysaur')+1,pokemons.index('Bulbsaur')+1}
        labelframe=LabelFrame(self.canvas,text="",width=1500,height=800,highlightcolor="yellow",highlightbackground="red",highlightthickness=10)  
        labelframe.grid(padx = 0, pady = 0)
        def destroy():
            labelframe.destroy()
        self.mybutton2 = tk.Button(labelframe, text='Play', width=50, height=5, command=destroy)
        self.mybutton2.place(x=0,y=0)
    


        pokemon_names = []
        mixer.music.load('Muisc (1).wav')
        mixer.music.play(-1)


        def button_event():
            Frame1=[]
            Frame2=[]
            Frame3=[]
            for i in range(1,10): #print out every image object's coordinates
                #print(i, self.canvas.coords(i))
                if self.canvas.coords(i)[0] >= 350 and self.canvas.coords(i)[0] <= 600 and self.canvas.coords(i)[1] >= 50 and self.canvas.coords(i)[1] <= 300:
                    Frame1.append(i)
                if self.canvas.coords(i)[0] >= 650 and self.canvas.coords(i)[0] <= 900 and self.canvas.coords(i)[1] >= 50 and self.canvas.coords(i)[1] <= 300:
                    Frame2.append(i)
                if self.canvas.coords(i)[0] >= 950 and self.canvas.coords(i)[0] <= 1200 and self.canvas.coords(i)[1] >= 50 and self.canvas.coords(i)[1] <= 300:
                    Frame3.append(i)
            TP=0
            FP=0
            Level = 1



          
            

            for i in Frame1:
                for j in Frame1:
                    if i<j:
                        if (i in set1 and j in set1) or (i in set2 and j in set2) or (i in set3 and j in set3):
                            TP+=1
                        else:
                            FP+=1

            Undiscovered=0
            for i in set1:
                for j in set1:
                    if i<j:
                        Undiscovered+=1
                        if (i in Frame1 and j in Frame1) or (i in Frame2 and j in Frame2) or (i in Frame3 and j in Frame3):
                            Undiscovered-=1

            for i in Frame2:
                for j in Frame2:
                    if i<j:
                        if (i in set1 and j in set1) or (i in set2 and j in set2) or (i in set3 and j in set3):
                            TP+=1
                        else:
                            FP+=1

            #Undiscovered=0
            for i in set2:
                for j in set2:
                    if i<j:
                        Undiscovered+=1
                        if (i in Frame1 and j in Frame1) or (i in Frame2 and j in Frame2) or (i in Frame3 and j in Frame3):
                            Undiscovered-=1




            

            for i in Frame3:
                for j in Frame3:
                    if i<j:
                        if (i in set1 and j in set1) or (i in set2 and j in set2) or (i in set3 and j in set3):
                            TP+=1
                        else:
                            FP+=1

            #Undiscovered=0
            for i in set3:
                for j in set3:
                    if i<j:
                        Undiscovered+=1
                        if (i in Frame1 and j in Frame1) or (i in Frame2 and j in Frame2) or (i in Frame3 and j in Frame3):
                            Undiscovered-=1

            self.canvas.itemconfigure(self.text_id,text="Score: "+str(TP*2-FP-Undiscovered))
            #self.canvas.itemconfigure(self.text_id2,text="Incorrect: "+str(FP))
            #self.canvas.itemconfigure(self.text_id3,text="Undiscovered: "+str(Undiscovered))
            #self.canvas.itemconfigure(self.text_id1,text="Level: "+str(Level))
            #print("TP=", TP, "FP=", FP, "Undiscovered=", Undiscovered)



                    #if self.canvas.coords(i)[0] >= 400 and self.canvas.coords(i)[0] <= 650 and self.canvas.coords(i)[1] >= 400 and self.canvas.coords(i)[1] <= 650:
                        #print(yes)


                      #print(self.image_1.x, self.image_1)
                
            #print(self.image_2.xpos, self.image_2.ypos)
            #print(self.image_3.xpos, self.image_3.ypos)
            #print(self.image_4.xpos, self.image_4.ypos)
            #print(self.image_5.xpos, self.image_5.ypos)
            #print(self.image_6.xpos, self.image_6.ypos)
        self.mybutton = tk.Button(self, text='Calculate your score', width=50, height=5, command=button_event)
        self.mybutton.place(x=600,y=600)
    def close(self):
        print("Application-Shutdown")
        self.master.destroy()
def main():
    app_win = tk.Tk()
    app_win.title(APP_TITLE)
    app_win.geometry("+{}+{}".format(APP_XPOS, APP_YPOS))

    #app_win.geometry("{}x{}".format(APP_WIDTH, APP_HEIGHT))
    #def button_event():
    #    print(app_win.)
#    mybutton = tk.Button(app_win, text='Calculate', command=button_event)#   mybutton.pack()
    app = Application(app_win).pack(fill='both', expand=True)
    app_win.mainloop()
if __name__ == '__main__':
    main()
#/usr/bin/env python
#-*-编码:utf-8-*-
导入pygame
导入时间
从PIL导入图像,ImageTk
随机输入
从pygame导入混合器
从tkinter进口*
尝试:
#Tkinter for Python 2.xx
将Tkinter作为tk导入
除恐怖外:
#Tkinter for Python 3.xx
将tkinter作为tk导入
APP_TITLE=“口袋妖怪游戏”
APP_XPOS=100
APP_YPOS=100
APP_宽度=300
APP_高度=200
IMAGE\u PATH=“images/”
mixer.init()
类CreateCanvasObject(对象):
定义初始化(自我、画布、图像名称、XPO、YPO):
self.canvas=画布
self.image\u name=image\u name
self.xpos,self.ypos=xpos,ypos
#image=image.open(“C:\\Users\\happy\\Desktop\\test.jpg”)
#photo=ImageTk.PhotoImage(图像)
self.tk_image=ImageTk.PhotoImage(
file=“{}{}”。格式(图像路径、图像名称))
self.image\u obj=canvas.create\u image(
XPO,YPO,image=self.tk_image)
#self.tk_image=tk.PhotoImage(
#file=“{}{}”。格式(图像路径、图像名称))
#self.image\u obj=canvas.create\u image(
#XPO,YPO,image=self.tk_image)
canvas.tag_bind(self.image_obj',self.move)
canvas.tag_bind(self.image_obj',self.release)
self.move_标志=False
#self.text_id=self.canvas.create_text(950,50,font='Times 24',text=”“)
def移动(自身、事件):
#self.image_obj[0]:
#打印(self.image_1.x)
如果self.move_标志:
new_xpos,new_ypos=event.x,event.y
self.canvas.move(self.image_obj,
新的xpos-self.mouse-xpos,新的xpos-self.mouse-ypos)
self.mouse\u xpos=new\u xpos
self.mouse\u ypos=新的\u ypos
其他:
self.move_标志=真
self.canvas.tag\u raise(self.image\u obj)
self.mouse\u xpos=event.x
self.mouse_ypos=event.y
def释放(自身、事件):
self.move_标志=False
类应用程序(tk.Frame):
定义初始(自我,主):
#打印(自我)
self.master=master
self.master.protocol(“WM_删除_窗口”,self.close)
tk.Frame.\uuuuu init\uuuuuuu(自,主)
self.canvas=tk.canvas(self,宽度=400,高度=400,背景=“#AFFAAF”,
Highlight厚度=0)
self.canvas.pack(fill=“both”,expand=True)
口袋妖怪=[“Charizard”、“Charmeleon”、“Charmander”、“Blastoise”、“Wartortle”、“Squirtle”、“Venusaur”、“Ivysaur”、“Bulbsaur”]
随机。洗牌(口袋妖怪)
对于范围(0,9)内的i:
self.image_1=createCanvaObject(self.canvas,口袋妖怪[i]+'.png',100*i+350500)
#self.image_2=createCanvaObject(self.canvas,“Charmeleon.png”,200500)
#self.image_3=createCanvaObject(self.canvas,“Charmander.png”,300500)
#self.image_4=createCanvaObject(self.canvas,“Blastoise.png”,400500)
#self.image_5=createCanvaObject(self.canvas,“warortle.png”,500500)
#self.image_6=createCanvaObject(self.canvas,“Squirtle.png”,600500)
#self.image_7=CreateCanvasObject(self.canvas,“Venusaur.png”,700500)
#self.image_8=CreateCanvasObject(self.canvas,“Ivysaur.png”,800500)
#self.image_9=CreateCanvasObject(self.canvas,“Bulbsaur.png”,900500)
self.canvas.create_矩形(35050600300)#fill='white')
canvas.create_矩形(650,50900300)#,fill='white')
self.canvas.create_矩形(950、50、1200、300)#,fill='white')
self.canvas.create_矩形(3205501250450)
self.text_id1=self.canvas.create_text(500750,font='Times 24',text=”“)
self.text_id=self.canvas.create_text(650750,font='Times 24',text=”“)
self.text_id2=self.canvas.create_text(800750,font='Times 24',text=”“)
self.text_id3=self.canvas.create_text(1000750,font='Times 24',text=”“)
self.text_id4=self.canvas.create_text(800400,font='Times 24',text=“将口袋妖怪拖到正确的进化位置。完成后,按[Calculate]”)
set1={pokemons.index('Charizard')+1,pokemons.index('Charmeleon')+1,pokemons.index('Charmander')+1}
set2={pokemons.index('Blastoise')+1,pokemons.index('Wartortle')+1,pokemons.index('Squirtle')+1}
set3={pokemons.index('Venusaur')+1,pokemons.index('Ivysaur')+1,pokemons.index('Bulbsaur')+1}
labelframe=labelframe(self.canvas,text=,宽度=1500,高度=800,highlightcolor=“黄色”,highlightbackground=“红色”,highlightthickness=10)
labelframe.grid(padx=0,pady=0)
def destroy():
labelframe.destroy()
self.mybutton2=tk.Button(labelframe,text='Play',width=50,height=5,command=destroy)
self.mybutton2.place(x=0,y=0)
口袋妖怪的名字=[]
mixer.music.load('Muisc(1.wav'))
混音器。音乐。播放(-1)
def按钮_事件():
框架1=[]
框架2=[]
框架3=[]
对于范围(1,10)内的i:#打印出每个图像对象的坐标
#打印(i,self.canvas.coords(i))
如果self.canvas.coords(i)[0]>=350,self.canvas.coords(i)[0]=50,self.canvas.coords(i)[0]=50,self.canvas.coords(i)[1]=950,self.canvas.coords(i)[0]=50,self 1]这对您有帮助吗-


使用.place\u-forget()

因为你使用了
。在按钮上放置(…)
,你需要使用
.place\u-forget()
来移除它。那么你知道如何显示按钮吗