Button Help - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Button Help (/thread-3113.html) |
Button Help - slopz - Apr-29-2017 I have created a choice-based game in Python for a project in my AP Computer Science class. It works, for the most part, but I am having trouble getting the buttons to disappear once they are pressed. Help would be much obliged. def new_1_button(frame,text,function): #frame.stop() #frame = simplegui.create_frame('CYOA', FRAMEWIDTH,FRAMEHEIGHT) hp_color() #frame.set_draw_handler(draw_handler) frame.set_canvas_background('black') #frame.start() frame.add_button(text, function) def new_2_button(frame, text_1, func_1, text_2, func_2): #frame.stop() #frame = simplegui.create_frame('CYOA', FRAMEWIDTH,FRAMEHEIGHT) hp_color() frame.set_draw_handler(draw_handler) frame.set_canvas_background('black') #frame.start() frame.add_button(text_1,func_1) frame.add_label(" ") frame.add_button(text_2,func_2)This is the code for the buttons. RE: Button Help - nilamo - Apr-29-2017 Do you have a small snippet that can actually be run? Or at least help us out by letting us know what gui framework you're using? RE: Button Help - slopz - Apr-29-2017 This is my entire code import SimpleGUICS2Pygame.simpleguics2pygame as simplegui import random FRAMEWIDTH = 800 FRAMEHEIGHT = 500 buttonArray = [] frame = simplegui.create_frame('CYOA', FRAMEWIDTH, FRAMEHEIGHT) hp = 100 hp_topleft = (FRAMEWIDTH+105, 5) hp_bottomleft = (FRAMEWIDTH+105, 25) hp_br = (FRAMEWIDTH+105 + hp, 25) hp_tr = (FRAMEWIDTH+105 + hp, 5) hp_num = "Health: " + str(hp) step1 = True step2 = False step3 = False step4 = False step5 = False step6 = False step7 = False planb = False left = False end = False death = [False, True] text1 = "The Date is October 28, 1962..." text2 = "Welcome To The End" enemy_list = ["Bandits", "Patrol Caravans", "Ferril Beasts"] enemy_item = ["bone", "Hand Gun", "Machine Gun", "Vehicle", "Gasoline"] enemypick = random.choice(enemy_list) deathpick = False barcolor = "red" def hp_add(number): global hp, hp_num if (hp + number) < 100: hp += number else: hp = 100 hp_num = "Health: " + str(hp) def hp_minus(number): global hp, hp_num if hp > 0: hp = number else: hp = 0 hp_num = "Health: " + str(hp) def hp_percent(boolean, num): if boolean == True: hp_add(int(hp*(.01*num))) elif boolean == False: hp_minus(int(hp*(.01*num))) def hp_color(): global hp, barcolor, hp_br, hp_tr if hp >= 75 and hp <= 100: barcolor = "red" hp_br = ((FRAMEWIDTH + 105) + hp, 25) hp_tr = ((FRAMEWIDTH + 105) + hp, 5) elif hp < 75 and hp >= 50: barcolor = "indian red" hp_br = ((FRAMEWIDTH + 105) + hp, 25) hp_tr = ((FRAMEWIDTH + 105) + hp, 5) elif hp < 50 and hp >=25: barcolor = "light coral" hp_br = ((FRAMEWIDTH + 105) + hp, 25) hp_tr = ((FRAMEWIDTH + 105) + hp, 5) elif hp < 25 and hp >=0: barcolor = "snow 2" hp_br = ((FRAMEWIDTH + 105) + hp, 25) hp_tr = ((FRAMEWIDTH + 105) + hp, 5) elif hp > 100: while hp > 100: hp_minus(1) hp_br = ((FRAMEWIDTH + 105) + hp, 25) hp_tr = ((FRAMEWIDTH + 105) + hp, 5) hp_color() elif hp < 0: while hp < 0: hp_add(1) hp_br = ((FRAMEWIDTH + 105) + hp, 25) hp_tr = ((FRAMEWIDTH + 105) + hp, 5) hp_color() def text_set(line1,line2): global text1, text2 text1= line1 text2= line2 def startpage(): global step1,step2 step1 = False step2 = True buttons(frame) text_set("You emerge, somehow,","alive from rubble.") def left_1(): global step2,step3, planb, left hp_minus(1) step2 = False step3 = True planb=True left = True buttons(frame) text_set("You approach a","seemingly empty house.") def right_1(): global step2,step3,left hp_minus(25) step2 = False step3 = True left = False buttons(frame) text_set("You enter your once beloved hometown","reduced to rubble.") def store(): global step3, step4, planb hp_percent(True,3) step3 = False step4 = True planb = False buttons(frame) text_set("You find medicine and","food from a store.") def enterhome(): global step3, step4, planb step3 = False step4 = True planb = True hp_percent(False,27) buttons(frame) text_set("You are approached", "by a deadly group of " + enemypick + ".") def trail(): global step3, step4, planb step3 = False step4 = True planb = False hp_percent(False,12) buttons(frame) text_set("You stumble upon a","military sanctum.") def schrodinger(): global death,deathpick deathpick = random.choice(death) if deathpick == False: hp_percent(False,25) text_set("Emerge victorious with a ", enemypick +" "+ random.choice(enemy_item)+ ".") elif deathpick == True and (hp == 0 or hp < 0): hp_minus(hp) text_set("You have caught","a severe case of dead.") elif deathpick == True and (hp != 0 and hp >0): hp_minus(hp) text_set("You are challenged by","a smelly group of foes!") def fight_enemies(): global step4, step5, planb step4 = False step5 = True planb = True schrodinger() buttons(frame) def doc(): global step5, step6, planb, left step5 = False step6 = True planb = True left = False hp_percent(True, 90) buttons(frame) text_set("You meet an esteemed doctor","and gain extensive knowledge.") def student(): global step6,step7,planb step6 = False step7 = True planb = True buttons(frame) text_set("You now understand","the wonders of medicine.") def locals(): global end, step4 step4 = False end = True buttons(frame) text_set("You encounter","a group of","hungry locals.") def restart(): global step1,step2,step3,step4,step5,step6,step7,planb,deathpick,hp,enemypick,end,stay hp_add(101) step1 = False step2 = True step3 = False step4 = False step5 = False step6 = False step7 = False planb = False deathpick = False left = False end = False stay = False enemypick = random.choice(enemy_list) text_set("You emerge, somehow,","alive from rubble.") buttons(frame) def goback(): global step1,step2,step3,step4,step5,step6,step7,planb,deathpick,hp,enemypick,end,stay step1 = False step2 = True step3 = False step4 = False step5 = False step6 = False step7 = False planb = False deathpick = False left = False end = False stay = False enemypick = random.choice(enemy_list) text_set("You ponder the desolate","wasteland ahead.") buttons(frame) def new_1_button(frame,text,function): #frame.stop() #frame = simplegui.create_frame('CYOA', FRAMEWIDTH,FRAMEHEIGHT) hp_color() #frame.set_draw_handler(draw_handler) frame.set_canvas_background('black') #frame.start() frame.add_button(text, function) def new_2_button(frame, text_1, func_1, text_2, func_2): #frame.stop() #frame = simplegui.create_frame('CYOA', FRAMEWIDTH,FRAMEHEIGHT) hp_color() frame.set_draw_handler(draw_handler) frame.set_canvas_background('black') #frame.start() frame.add_button(text_1,func_1) frame.add_label(" ") frame.add_button(text_2,func_2) def no_button(frame): #frame.stop() #frame = simplegui.create_frame('CYOA', FRAMEWIDTH,FRAMEHEIGHT) hp_color() #frame.set_draw_handler(draw_handler) frame.set_canvas_background('black') #frame.start() def cyoa(canvas): hp_color() canvas.draw_text(text1,(FRAMEWIDTH/4,FRAMEHEIGHT/2),30,'white') canvas.draw_text(text2,(FRAMEWIDTH/4,2*FRAMEHEIGHT/3),30,'white') def death1(): global hp if hp == 0: text_set("You are chellenged by","a smelly group of foes!") def buttons(frame): if step1 == True: button1 = frame.add_button("Enter your fate", startpage, 50) if step2 == True: new_2_button(frame,"Head... West?", left_1,"Head... East?",right_1) if step3 == True and planb == False and left == False: new_2_button(frame,"Follow a highway.",trail,"Follow a road.",doc) if step3 == True and planb == False and left == True: new_1_button(frame,"Enter the store.", store) if step3 == True and planb == True and left == True: new_2_button(frame,"Seek refuge in the home.", enterhome,"Seek refuge elsewhere.",store) if step4 == True and planb == False and left == True: new_1_button(frame,"Trek on.",doc) if step4 == True and planb == True and left == True: new_2_button(frame,"Conquer the " + enemypick + ".",fight_enemies,"Run like the coward you are.",store) if step4 == True and planb == False and left == False: new_2_button(frame,"Interact with the locals.",locals,"Return from whence you came.",goback) if step5 == True and planb == True and deathpick == False and left == True: new_1_button(frame,"Emerge victorious with the items you pocketed.", goback) if (step5 == True and planb == True and deathpick == True and left == True and hp == 0) or hp == 0: new_1_button(frame,"This must be your first nucelar holocaust.", restart) if step5 == True and planb == True and deathpick == True and left == True and hp != 0: new_1_button(frame,"Run for your life!",store) if step6 == True and planb == True and left == False: new_2_button(frame,"Become the doctor's student.",student,"Return with little knowledge of medicine.", goback) if step7 == True and planb == True and left == False: new_1_button(frame,"Return to teach your new knowledge of medicine.",goback) if end == True: no_button(frame) #buttons(frame) def myButtonHandler(): messagebox.showinfo("Title", "a Tk MessageBox") def draw_handler(canvas): global barcolor,hp_br,hp_tr canvas.draw_polygon([hp_topleft, hp_bottomleft, hp_br, hp_tr], 3, barcolor, barcolor) canvas.draw_text(hp_num,(FRAMEWIDTH+150,20),12, barcolor) cyoa(canvas) death1() #frame.add_button("hello", myButtonHandler) buttons(frame) frame.set_draw_handler(draw_handler) frame.start() frame.set_canvas_background('black') RE: Button Help - OPi - Apr-10-2021 I just found this old thread. I'm the developer of SimpleGUICS2Pygame (that is a reimplementation of specific modules from CodeSkulptor). If I well understood you wanted remove buttons before add new ones. Unfortunately it is not possible. The API of SimpleGUI (in CodeSkulptor) is not designed for do that. But it is possible to change text in buttons with the method set_text(). So maybe you can do want you want by change your logic. |