如何在调用按钮后获取按钮的属性

时间:2014-08-19 10:49:31

标签: python tkinter

好的,所以我为此做了一个帐户,因为这是我的大学项目,如果有人可以帮助我,我会非常感激。基本上我正在尝试为Enigma机器创建一个界面,我已经将所有内容都放在了我想要的地方,但是我无法通过按钮将文本打印到文本框中。因为我在一个循环中有它我不是单独设置每个按钮来打印一些东西,如果我尝试使用带有列表的计数器它不会工作因为它总是打印列表中的最后一个值。继承我的代码:

    from Tkinter import *

buttonlist = ["","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""]

master = Tk()
tempval = 0
Grid.rowconfigure(master,0,weight=1)
Grid.columnconfigure(master,0,weight=1)

def val_click(val):
    current_output = outputtext.get()
    inputtext.delete(0, END)
    inputtext.insert(0, current_output+str(val))


textlabel = Label(master, text="INPUT", fg="black")
textlabel.grid(row=0, column=0, columnspan=10)
textlabel2 = Label(master, text="OUPUT", fg="black")
textlabel2.grid(row=2, column=0, columnspan=10)
inputtext = Entry(master, width=50, bg="light blue")
inputtext.grid(row=1, column=0, columnspan=10)
outputtext = Entry(master, width=50, bg="light yellow")
outputtext.grid(row=3, column=0, columnspan=10)
file = open("Tkinter information.txt", "r")
readlines = file.readlines()

for s in range(0,31):
    buttonting = str((readlines[5]).split(",")[tempval])
    buttonlist[s] = Button(master, text=str((readlines[0]).split(",")[tempval]), command=lambda: val_click("THIS IS WHERE I WANT TO PRINT THE TEXT PART OF THE BUTTON"), width=str((readlines[2]).split(",")[tempval]), height=1, bg=str((readlines[1]).split(",")[tempval]))
    buttonlist[s].grid(row=(readlines[3]).split(",")[tempval][0], column=(readlines[3]).split(",")[tempval][1], sticky=N+S+E+W, columnspan=(readlines[4]).split(",")[tempval])
    tempval = tempval + 1
    print buttonlist[s]


inputtext = Label(master, text="Rotor1", fg="black")
inputtext.grid(row=10, column=1, columnspan=2)
inputtext = Label(master, text="Rotor2", fg="black")
inputtext.grid(row=10, column=4, columnspan=2)
inputtext = Label(master, text="Rotor3", fg="black")
inputtext.grid(row=10, column=7, columnspan=2)
textbox = Entry(master, width=50, bg="light yellow")
textbox.grid(row=11, column=0, columnspan=3)
textbox = Entry(master, width=50, bg="light yellow")
textbox.grid(row=11, column=3, columnspan=4)
textbox = Entry(master, width=50, bg="light yellow")
textbox.grid(row=11, column=7, columnspan=3)

主循环()

这部分来自记事本,它从中提取信息:

Q,W,E,R,T,Y,U,I,O,P,A,S,D,F,G,H,J,K,L,Z,X,C,V,B,N,M,SPACE,CLEAR,ENCRYPT,SET ROTOR,SET PLUGBOARD,
light green,light green,light green,light green,light green,light green,light green,light green,light green,light green,light green,light green,light green,light green,light green,light green,light green,light green,light green,light green,light green,light green,light green,light green,light green,light green,light green,red,light blue,yellow,green,
12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12
40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,60,61,62,63,64,65,66,67,70,72,74,76
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2
button001,button002,button003,button004,button005,button006,button007,button008,button009,button010,button011,button012,button013,button014,button015,button016,button017,button018,button019,button020,button021,button022,button023,button024,button025,button026,button027,button028,button029,button030,button031

所以我有一个想法,试图获取按钮的文本,并具有它将打印的内容,但我不知道如何获取按钮的文本。我的意思是说" A"按下它将在文本框中打印A但我不知道如何按下按钮的文本属性。如果有人知道请回复谢谢。

2 个答案:

答案 0 :(得分:1)

您可以创建一个值为“烘焙”的lambda(计算机术语:“bound”)。例如:

foo = "x"
Button(..., command= lambda the_value=foo: print "the value is " + the_value)

在你的代码中,它看起来像这样:

for s in range(0,31):
    ...
    s = str((readlines[0]).split(",")[tempval])
    buttonlist[s] = Button(master, text=s, command=lambda value=s: val_click(value), ...)
    ...

答案 1 :(得分:0)

你可能会想做这样的事情:

from Tkinter import *

master = Tk()
# You don't have to type "" every time, use *
buttonlist = [""]*4

# Create a StringVariable for the label
textvar = StringVar()
# Set it to 'INPUT'
textvar.set('INPUT')
# Create the Label using the StringVariable
textlabel = Label(master, textvariable=textvar, fg="black").pack(fill='both')

# Loop to make the Buttons
for i in range(4):
    # Create a StringVariable for each of the buttons
    buttonlist[i] = StringVar()
    # Set it to anything you want (I set it to the number)
    buttonlist[i].set(str(i))
    # Create the button, use the textvariable and create the command to set the textvar of the label to the text of the button
    # Note that the i=i in the lambda function is very important, if you miss this you will always get the value of the last button
    Button(master, textvariable=buttonlist[i], command=lambda i=i: textvar.set(buttonlist[i].get())).pack(fill='both')


master.mainloop()
编辑:在阅读了Bryan Oakley的回答后,我意识到按钮的StringVariables不是必需的。我认为Entry的StringVariable可能仍然是一个好主意。我可能会这样做:

from Tkinter import *

master = Tk()

# Create a StringVariable for the label
textvar = StringVar()
# Set it to 'INPUT'
textvar.set('INPUT')
# Create the Entry using the StringVariable
inputtext = Entry(master, width=50, textvariable=textvar, bg="light blue", justify='center').pack(fill='both')

def val_click(val):
    textvar.set(val)
    # Any other things you want to do here

strings = ['A', 'B', 'C', 'D']

# Loop to make the Buttons
for val in strings:
    # Make sure val is a string
    val=str(val)
    # Create the button, use the value and create the command to send the text to val_click
    Button(master, text=val, command=lambda t=val: val_click(t)).pack(fill='both')

master.mainloop()