繁体   English   中英

将消息框连接到按钮

[英]Connect a message box to a button

我希望能够单击按钮并使消息框显示生成的代码。 这是代码的一部分:

global s
letters = [random.choice('BCDFGHJKMPQRTVWXYYZ')  for x in range(19)]
numbers = [random.choice('2346789') for x in range(6)]
s = letters + numbers
random.shuffle(s)
s = ''.join(s)

global Code
Code = Entry(state='readonly')

def callback():
    Code = Entry(state='readonly', textvariable=s)

Code.grid(row=0, pady=20)
generate=PhotoImage(file='generate.gif')
G = Button(image=generate , command=callback, compound=CENTER)
G.grid(row=1, padx=206.5, pady=20) 

修正了一些带有注释的内容:

from Tkinter import *
import random
root = Tk()

letters = [random.choice('BCDFGHJKMPQRTVWXYYZ')  for x in range(19)]
numbers = [random.choice('2346789') for x in range(6)]
s = letters + numbers
random.shuffle(s)
s = ''.join(s)

# Rather than use global variables, which is generally a bad idea, we can make a callback creator function, which takes the formerly global variables as arguments, and simply uses them to create the callback function.
def makeCallback(sVariable):
    def callback():
# This will set the text of the entry
        sVar.set(s)
    return callback

# Use a StringVar to alter the text in the Entry
sVar = StringVar(root)
# You can use an Entry for this, but it seems like a Label is more what you're looking for.
Code = Entry(root, state='readonly', textvariable=sVar)

# Create a callback function
callback = makeCallback(sVar)

Code.grid(row=0, pady=20)
generate=PhotoImage(file='generate.gif')
G = Button(root, image=None , command=callback, compound=CENTER)
G.grid(row=1, padx=206.5, pady=20) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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