如果"一个"我怎么能隐藏所有按钮?按下按钮?那可能吗?
这是我的代码以及我尝试的所有内容:
import sys
from Tkinter import *
from ttk import Frame, Button, Label, Style
from ttk import Entry
import Tkinter
class AllButtons(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def callback(self, number):
print "click!", self, number
def hide_me(self, event):
event.widget.grid_forget(), self, event
def initUI(self):
self.parent.title("mGUI")
Style().configure("TButton", padding=(0, 5, 0, 5),
font='serif 20', background="Black", foreground="darkorange2",
activebackground="red",activeforeground="red")
self.columnconfigure(0, pad=3)
self.columnconfigure(1, pad=3)
self.columnconfigure(2, pad=3)
self.columnconfigure(3, pad=3)
self.rowconfigure(0, pad=3)
self.rowconfigure(1, pad=3)
self.rowconfigure(2, pad=3)
self.rowconfigure(3, pad=3)
self.rowconfigure(4, pad=3)
Funk_1 = Button(self, text="Funk_1", command=lambda:self.callback(1))
Funk_1.grid(row=1, column=0)
Funk_2 = Button(self, text="Funk_2", command=lambda:self.callback(2))
Funk_2.grid(row=1, column=1)
Funk_3 = Button(self, text="Funk_3", command=lambda:self.callback(3))
Funk_3.grid(row=1, column=3)
Funk_4 = Button(self, text="Funk_4", command=lambda:self.callback(4))
Funk_4.grid(row=3, column=0)
Funk_5 = Button(self, text="Funk_5", command=lambda:self.callback(5))
Funk_5.grid(row=3, column=1)
Funk_6 = Button(self, text="Funk_6", command=lambda:self.callback(6))
Funk_6.grid(row=3, column=3)
Funk_7 = Button(self, text="Funk_7", command=lambda:self.callback(7))
Funk_7.grid(row=5, column=0)
Funk_8 = Button(self, text="Funk_8", command=lambda:self.callback(8))
Funk_8.grid(row=5, column=1)
Funk_9 = Button(self, text="Funk_9", command=lambda:self.callback(9))
Funk_9.grid(row=5, column=3)
Funk_1.bind('<Button-1>', self.hide_me)
Funk_2.bind('<Button-1>', self.hide_me)
Funk_3.bind('<Button-1>', self.hide_me)
Funk_4.bind('<Button-1>', self.hide_me)
self.pack()
def main():
mGUI = Tk()
app = AllButtons(mGUI)
mGUI.mainloop()
if __name__ == '__main__':
main()
答案 0 :(得分:0)
您可以控制按钮的状态。如果您使用&#39; state = normal&#39;初始化按钮,则可以使用其他按钮调用禁用该按钮的方法。
self.Funk_1 = Button(self, text="Funk_1", state='normal', command=lambda:self.set_buttons)
self.Funk_1.grid(row=1, column=0)
def set_buttons(self):
self.Funk_1.configure(state='disabled')
你可以在这里阅读更多内容; http://effbot.org/tkinterbook/button.htm
答案 1 :(得分:0)
最简单的方法是将按钮的ID存储在列表中,然后对列表进行操作。您的代码仅限于按钮及其回调。
from Tkinter import *
from functools import partial
class AllButtons():
def __init__(self, parent):
self.parent = parent
self.initUI()
Button(self.parent, text="Exit", command=self.parent.quit,
bg="orange").grid(row=20, column=2)
def callback(self, number):
""" removes all buttons not clicked
"""
number=int(number)
print "click!", number
for ctr in range(len(self.button_list)):
if ctr != number: ## leave the button clicked
print "Deleting button number", ctr+1
self.button_list[ctr].grid_forget()
def initUI(self):
""" create 9 buttons and store their Tkinter ID's in a list
"""
self.parent.title("mGUI")
self.button_list=[]
g_row=1
g_col=1
for ctr in range(9):
## creating button located at offset "ctr" in the list
## this is also the number sent to the callback
funk=Button(self.parent, text="Funk_%d" % (ctr+1),
command=partial(self.callback, ctr))
## add each button's id to the list
self.button_list.append(funk)
funk.grid(row=g_row, column=g_col)
g_col += 1
if g_col > 3:
g_row += 2
g_col=1
if __name__ == '__main__':
mGUI = Tk()
app = AllButtons(mGUI)
mGUI.mainloop()