简体   繁体   中英

How do I get my code to end a funtion after a button is pressed?

# import engines for code hs (Existing)
import pip
import os
import sys,time,random
import tkinter
from tkinter import *
root = Tk()

# establishes cavas as a object (Existing)
screen = Canvas(root,width = 500, height = 600, background = "light blue")
screen.pack()

# Define some global variable (New)
number = 0
""" funtaion that runs when a button is pressed returning a number
value base on what they choose and then disables the buttons""" 

def helloCallBack(num,b1,b2,b3,b4):

How do I make the program recognize the variable global? because they currently it is not working. global number number= num

    """if b1 or b2 or b3 or  b4 != 0:
        b1['state']='disabled'
        b2['state']='disabled'
        b3['state']='disabled'
        b4['state']='disabled'"""
    for b in (b1,b2,b3,b4): 
        if b:  b['state'] = 'disabled'
        
        
        
        
    



        
    
           
   
   
   
   

creats up to four buttons based on the value assighned to num value the opt1-4 values assighned the dialouge to the buttons

def button_maker(num_buttons,opt1,opt2,opt3,opt4):
    while True:
    
        master=tkinter.Tk()
        master.title("grid() method")
        master.geometry("600x150")

if num_opt is equal to 1 create one buttons

        if num_buttons == 1:
            button1=tkinter.Button(master, text= opt1,command=lambda :  helloCallBack(1,button1,0,0,0))
            button1.grid(row=1,column=1)
        
            
        
        
        
        
        
       
        
        

if num_opt is equal to 2 create two buttons

        elif num_buttons==2:

            button1=tkinter.Button(master, text= opt1,command=lambda :  helloCallBack(1,button1,0,0,0))
            button1.grid(row=1,column=0)
        
        
            button2=tkinter.Button(master, text= opt2,command=lambda :  helloCallBack(2,button1,button2,0,0))
            button2.grid(row=6,column=0)
       
        # if num_opt is equal to 3 create three buttons
        elif num_buttons==3:
            button1=tkinter.Button(master, text=opt1,command=lambda :  helloCallBack(1,button1))
            button1.grid(row=1,column=0)
        
        
        

            button2=tkinter.Button(master, text=opt2, command=lambda :  helloCallBack(2,button1,button2,button3,button4))
            button2.grid(row=6,column=0)
        
        

            button3=tkinter.Button(master, text=opt3,command=lambda :  helloCallBack(3,button1,button2,button3,button4))
            button3.grid(row=8,column=0)
        
      # if num_opt is 4 or over four buttons will be made
        else:
            button1=tkinter.Button(master, text=opt1,command=lambda : helloCallBack(1,button1,button2,button3,button4))
            button1.grid(row=1,column=0)
        
        

            button2=tkinter.Button(master, text=opt2,command=lambda :helloCallBack(2,button1,button2,button3,button4))
            button2.grid(row=6,column=0)
        
        

            button3=tkinter.Button(master, text=opt3,command=lambda : helloCallBack(3,button1,button2,button3,button4))
            button3.grid(row=8,column=0)
            
       
        

            button4=tkinter.Button(master, text=opt4,command=lambda : helloCallBack(4,button1,button2,button3,button4))
            button4.grid(row=10,column=0)
        root.mainloop()
         
            
           
           
        
    
        
       


    

cuases text to be type out slow typing_speed = 50 #wpm def slow_type(t): for l in t: sys.stdout.write(l) sys.stdout.flush() time.sleep(random.random()*10.0/typing_speed) print('')

saves a diolouge tree and prints the text out one by one by slow typing it."""

def print_text(my_list):
    b=0
    space=input("press enter key to continue." )
    #key = event.keysym
    #print(event.keysym)
    for i in my_list:
        if space == "":
            slow_type(my_list[b])
            space=input("")
            b=b+1



print_text(["Excellent chioce Mr...","MMM...Mr..."])
#test number value
#print(number)
#test failed

"""creats four buttons on screan when clicked they deactivate and return a number value."""
button_maker(4,str("Tell them your real name!"),"Give them a fake identity ","\"None of your business! \"","\"...\"")


print(number)   

# is meant to give different adventures based on what the user chooses

unfortunately, it is not working as the code freezes after the buttons are made. if number==1: print(input("Make up a first and last name:"))

# please !!!SAVE ME!!!

In python, global variable must be declared first before it can be used in functions, so you should add a piece of code at the start to declare that:

# import engines for code hs (Existing)
import pip
import os
import sys,time,random
import tkinter
from tkinter import *
root = Tk()

# establishes cavas as a object (Existing)
screen = Canvas(root,width = 500, height = 600, background = "light blue")
screen.pack()

# Define some global variable (New)
number = 0

However, there seem to be typo in the function button_maker() where you have 4 variable p, k, n, l not defined, which returns error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM