我正在使用wxPython构建一个包含大量切换按钮(25 x 25)的GUI。我已经使用GridSizer和AddMany创建切换按钮的网格。我无法使用AddMany语法将单个按钮分配给变量,因此,我不确定如何跟踪单击的切换按钮
这是我第一次尝试使用wxpython库构建应用程序,而我对它更强大的功能不是很熟悉。我过去的项目只涉及几个按钮,不需要大量的按钮
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context=".MainActivity">
<ImageView
android:id="@+id/top_logo"
android:layout_width="160dp"
android:layout_height="0dp"
android:layout_marginTop="16dp"
android:layout_weight="2"
android:src="@drawable/company_logo"/>
<!-- animate this image going up and expanding into top_logo -->
<ImageView
android:id="@+id/middle_logo"
android:layout_width="100dp"
android:layout_height="0dp"
android:layout_weight="1"
android:src="@drawable/company_logo"/>
<TextView
android:id="@+id/welcome_text"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="2"
android:text="@string/welcome_message"/>
</LinearLayout>
我在互联网上找不到很好的建议或示例,我希望wxpython的一些专家可以在这里为我提供帮助
答案 0 :(得分:0)
您可以使用label
或为每个按钮指定一个name
,同时将其当前值存储在dictionary
中。
例如:
import wx
class Example(wx.Frame):
def __init__(self, *args, **kwargs):
super(Example, self).__init__(*args, **kwargs)
self.InitUI()
def InitUI(self):
panel = wx.Panel(self)
self.button_dict = {'Info':False,'Error':False,'Question':False}
hbox = wx.BoxSizer()
sizer = wx.GridSizer(2, 2, 2, 2)
sizer.AddMany([wx.ToggleButton(panel, label='Info'), wx.ToggleButton(panel, label='Error'), wx.ToggleButton(panel, label='Question')])
hbox.Add(sizer, 0, wx.ALL, 15)
panel.SetSizer(hbox)
self.Bind(wx.EVT_TOGGLEBUTTON, self.ShowButton)
self.SetSize((300, 200))
self.SetTitle('Buttons')
self.Centre()
self.Show(True)
def ShowButton(self, event):
x = event.GetEventObject()
lab = x.GetLabel()
print("Button pressed",lab)
if self.button_dict[lab]:
self.button_dict[lab] = False
else:
self.button_dict[lab] = True
print(self.button_dict)
def main():
ex = wx.App()
Example(None)
ex.MainLoop()
if __name__ == '__main__':
main()