Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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
Button Python 3.3按钮隐藏编码不起作用_Button_Python 3.x - Fatal编程技术网

Button Python 3.3按钮隐藏编码不起作用

Button Python 3.3按钮隐藏编码不起作用,button,python-3.x,Button,Python 3.x,在我的游戏中,按下开始按钮后,我想隐藏按钮,以阻止用户再次按下开始按钮,并使更多的气泡出现。我试图使一个变量等于0。然后,每次单击“开始”按钮时,该值都会上升1。如果该值为2,则“开始”按钮将消失或变为非活动状态。通过尝试添加这个“隐藏按钮代码”,当单击开始按钮时,我的屏幕上只显示一个气泡。哦,启动按钮并没有隐藏 from tkinter import * import random import tkinter as tk # Message box for s

在我的游戏中,按下开始按钮后,我想隐藏按钮,以阻止用户再次按下开始按钮,并使更多的气泡出现。我试图使一个变量等于0。然后,每次单击“开始”按钮时,该值都会上升1。如果该值为2,则“开始”按钮将消失或变为非活动状态。通过尝试添加这个“隐藏按钮代码”,当单击开始按钮时,我的屏幕上只显示一个气泡。哦,启动按钮并没有隐藏

    from tkinter import *
    import random
    import tkinter as tk
    # Message box for score to tell user if they have won or lost
    from tkinter.messagebox import showinfo



    class BubbleFrame:


        def __init__(self, root, name):
            self.name = name
            root.title("Math Bubbles")
            self.bubbles = {} # this will hold bubbles ids, positions and velocities
            self.score = 0
            self.buttonclick = 0 # Newly added in attempt to hide button
            Button(root, text="Start", width=8, bg="Pink", command=self.make_bubbles).pack() # This button starts the game, making the bubbles move across the screen
            Button(root, text="Quit", width=8, bg="Yellow",command=quit).pack()
            self.canvas = Canvas(root, width=800, height=650, bg='#afeeee')
            self.canvas.create_text(400, 30, fill="darkblue", font="Times 20 italic bold", text="Click the bubbles that are answers in the two times tables.")
            #Shows score at beinging of the game
            self.current_score = self.canvas.create_text(200, 60, fill="darkblue", font="Times 15 italic bold", text="Your score is: 0")
            self.canvas.pack()



        def make_bubbles(self):
            for each_no in range(1, 21):
                self.buttonclick += 1 #Newly added in attempt to hide button
                xval = random.randint(5, 765)
                yval = random.randint(5, 615)
                COLOURS = ('#00ff7f', '#ffff00', '#ee82ee', '#ff69b4', '#fff0f5') # CAPS represents a constant variable
                colour = random.choice(COLOURS) # This picks a colour randomly
                oval_id = self.canvas.create_oval(xval, yval, xval + 60, yval + 60,fill=colour, outline="#000000", width=5, tags="bubble")
                text_id = self.canvas.create_text(xval + 30, yval + 30, text=each_no, tags="bubble")
                self.canvas.tag_bind("bubble", "<Button-1>", lambda x: self.click(x))
                self.bubbles[oval_id] = (xval, yval, 0, 0, each_no, text_id) # add bubbles to dictionary
                if buttonclick == 2: #Newly added in attempt to hide button
                    showinfo("Oh No", "Sorry %s, but the game is already started" % self.name)

        def click(self, event):
            if self.canvas.find_withtag(CURRENT):
                item_uid = event.widget.find_closest(event.x, event.y)[0]
                is_even = False
                try: # clicked oval
                    self.bubbles[item_uid]
                except KeyError: # clicked oval
                    for key, value in self.bubbles.iteritems():
                        if item_uid == value[5]: # comparing to text_id
                           if value[4] % 2 == 0:
                                is_even = True
                            self.canvas.delete(key) # deleting oval
                            self.canvas.delete(item_uid) # deleting text
                else:
                    if self.bubbles[item_uid][4] % 2 == 0:
                        is_even = True
                    self.canvas.delete(item_uid) # deleting oval
                    self.canvas.delete(self.bubbles[item_uid][5]) # deleting text

                if is_even:
                    self.score += 1
                else:
                    self.score -= 1
                    showinfo("Oh no!", "%s! You clicked the wrong bubble, please start again." % self.name)

                if self.score == 10:
                    #Tells user You won! if score is 10
                    showinfo("Winner", "You won %s!" % self.name)


            self.canvas.delete(self.current_score)
            #Shows updated score on canvas
            self.current_score = self.canvas.create_text(200, 60, fill="darkblue", font="Times 15 italic bold", text="Your score is: %s"%self.score)

        def bubble_move(self, root):
            for oval_id, (x, y, dx, dy, each_no, text_id) in self.bubbles.items():
                # update velocities and positions
                dx += random.randint(-1, 1)
                dy += random.randint(-1, 1)
                # dx and dy should not be too large
                dx, dy = max(-5, min(dx, 5)), max(-5, min(dy, 5))
                # bounce off walls
                if not 0 < x < 770:
                    dx = -dx
                if not 0 < y < 620:
                    dy = -dy
                # apply new velocities
                self.canvas.move(oval_id, dx, dy)
                self.canvas.move(text_id, dx, dy)
                self.bubbles[oval_id] = (x + dx, y + dy, dx, dy, each_no, text_id)
            # have mainloop repeat this after 100 ms
            root.after(100, self.bubble_move, root)

    if __name__ == "__main__":

        root = Tk()
        root.title("Welcome")
        Label(root, text="Welcome to Math bubbles, what is your name?").pack()
        name = Entry(root)
        name.pack()
        def submit(name, root):
        root.destroy()
        root = Tk()
        Label(root, text="Hello %s, press the Start button to begin.\n" % name).pack()
        BubbleFrame(root, name).bubble_move(root)
        Button(root, text="Ok", command=lambda: submit(name.get(), root)).pack()
        root.mainloop()
从tkinter导入*
随机输入
将tkinter作为tk导入
#记分信息框,告诉用户他们赢了还是输了
从tkinter.messagebox导入showinfo
类BubbleFrame:
定义初始化(self、root、name):
self.name=名称
root.title(“数学泡泡”)
self.bubbles={}#这将保存气泡ID、位置和速度
self.score=0
self.buttonclick=0#在尝试隐藏按钮时新添加
按钮(root,text=“Start”,width=8,bg=“Pink”,command=self.make_bubbles).pack()#此按钮启动游戏,使泡泡在屏幕上移动
按钮(root,text=“Quit”,width=8,bg=“Yellow”,command=Quit).pack()
self.canvas=canvas(根,宽=800,高=650,背景=“#afeee”)
self.canvas.create_text(400,30,fill=“darkblue”,font=“Times 20 italic bold”,text=“单击两个时间表中答案的气泡。”)
#在比赛结束时显示比分
self.current_score=self.canvas.create_text(200,60,fill=“深蓝色”,font=“乘以15斜体粗体”,text=“您的分数为:0”)
self.canvas.pack()
def生成气泡(自身):
对于范围(1,21)内的每个_编号:
self.button单击+=1#在尝试隐藏按钮时新添加
xval=random.randint(5765)
yval=random.randint(5615)
颜色=(“#00ff7f”、“#ffff00”、“#ee82ee”、“#ff69b4”、“#fff0f5”)#CAPS表示一个常量变量
颜色=随机。选择(颜色)#随机选择一种颜色
oval_id=self.canvas.create_oval(xval,yval,xval+60,yval+60,fill=colour,outline=“#000000”,width=5,tags=“bubble”)
text\u id=self.canvas.create\u text(xval+30,yval+30,text=each\u no,tags=“bubble”)
self.canvas.tag_bind(“气泡”,“lambda x:self.click(x))
self.bubbles[oval_id]=(xval,yval,0,0,每个_no,text_id)#将气泡添加到字典中
如果按钮单击==2:#在尝试隐藏按钮时新添加
showinfo(“哦,不”,“对不起%s,但是游戏已经开始了”%self.name)
def单击(自身,事件):
如果self.canvas.find_with tag(当前):
item\u uid=event.widget.find\u最近(event.x,event.y)[0]
是偶数=假吗
尝试:#单击椭圆形
self.bubbles[项目号]
除了KeyError:#单击椭圆
对于键,self.bubbles.iteritems()中的值:
如果项目_uid==值[5]:#与文本_id比较
如果值[4]%2==0:
是偶数吗
self.canvas.delete(键)#删除椭圆
self.canvas.delete(item_uid)#删除文本
其他:
如果self.bubbles[item_uid][4]%2==0:
是偶数吗
self.canvas.delete(item_uid)#删除椭圆
self.canvas.delete(self.bubbles[item_uid][5])#删除文本
如果是偶数:
self.score+=1
其他:
自我评分-=1
showinfo(“哦,不!”,“%s!您单击了错误的气泡,请重新开始。”%self.name)
如果self.score==10:
#告诉用户你赢了!如果分数是10
showinfo(“获胜者”,“您赢得了%s!”%self.name)
self.canvas.delete(self.current_分数)
#在画布上显示更新的分数
self.current\u score=self.canvas.create\u text(200,60,fill=“深蓝色”,font=“乘以15斜体粗体”,text=“您的分数是:%s”%self.score)
def气泡_移动(自身、根):
对于self.bubbles.items()中的椭圆编号(x,y,dx,dy,每个编号,文本编号):
#更新速度和位置
dx+=random.randint(-1,1)
dy+=random.randint(-1,1)
#dx和dy不应太大
dx,dy=max(-5,min(dx,5)),max(-5,min(dy,5))
#从墙上弹下来
如果不是0
我在第103行遇到了缩进错误,在第39行遇到了名称错误(它是
self.buttonclick
而不是
buttonclick
)。下次在发布前检查您的代码

你每次都会调用20个按钮
def make_bubbles(self):
    self.buttonclick += 1 #Newly added in attempt to hide button
    if self.buttonclick == 2: #Newly added in attempt to hide button
        self.startbutton.configure(state="disabled")
        showinfo("Oh No", "Sorry %s, but the game is already started" % self.name)
        return
    for each_no in range(1, 21):
        ...