python 我如何让我的代码在按下按钮后结束函数?

2ic8powd  于 2022-12-17  发布在  Python
关注(0)|答案(1)|浏览(165)
# 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):

我如何使程序识别变量全局?因为他们目前它是不工作.全局编号编号= 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'

根据指定给num值的值opt 1 -4值指定给按钮的值创建最多四个按钮

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

如果num_opt等于1,则创建一个按钮

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

如果num_opt等于2,则创建两个按钮

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()

导致文本被键入慢键入速度= 50 #wpm def慢类型(t):对于t中的l:系统标准输出写入(l)系统标准输出刷新()时间休眠(随机.随机()*10.0/打字速度)打印(“”)
保存一个对话树,并通过缓慢键入一个接一个地打印文本。""”

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

不幸的是,它不工作,因为代码冻结后,按钮是使.如果数字==1:print(input(“编个名和姓:“))

# please !!!SAVE ME!!!
nr9pn0ug

nr9pn0ug1#

在Python中,全局变量必须先声明,然后才能在函数中使用,因此应该在开头添加一段代码来声明:

# 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

但是,在函数button_maker()中似乎存在排印错误,您有4个变量p, k, n, l未定义,这将返回错误。

相关问题