首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WXPython事件处理-按钮

WXPython事件处理-按钮
EN

Stack Overflow用户
提问于 2021-09-22 00:20:58
回答 2查看 257关注 0票数 0

当我运行以下代码时,我收到了一个名为btn的按钮(图像附件),当我将它添加到我的右侧面板时,我无法移动它的位置,有人知道怎么做吗?另一个问题是,当我单击左面板上的时,我想调用这个特定的按钮,但是我不知道如何做到这一点,我试图把这个按钮放在OnSelChanged方法上,但是我无法让它工作。

整个代码都在下面,所以更容易复制-粘贴它。

代码语言:javascript
复制
import wx
from wx.core import Font, Position, Size
import wx.lib.newevent
from wx.lib.agw.customtreectrl import CustomTreeCtrl

def onButton(event):
    print("Button Pressed")

#---------------------------------------------------------------------------

class MyTree(wx.TreeCtrl):
    """
    Our customized TreeCtrl class.
    """
    def __init__(self, parent, id, position, size, style):
        """
        Initialize our tree.
        """
        wx.TreeCtrl.__init__(self, parent, id, position, size, style)
       
        root = self.AddRoot('Programmer')
        
        os = self.AppendItem(root, ' Big Script')
        pl = self.AppendItem(root, ' Big Script2')
        tk = self.AppendItem(root, 'Big Script3')
           
        cl = self.AppendItem(pl, 'Random Scripts')
        sl = self.AppendItem(pl, 'Miscelanious')

        self.AppendItem(os, 'Script1')
        self.AppendItem(os, 'Script2')
        self.AppendItem(os, 'Script3')
        self.AppendItem(os, 'Script4')
        self.AppendItem(cl, 'Script1')
        self.AppendItem(cl, 'Script2')
        self.AppendItem(cl, 'Script3')
        self.AppendItem(sl, '1')
        self.AppendItem(sl, '2')
        self.AppendItem(sl, '3')
        self.AppendItem(sl, '4')
        self.AppendItem(tk, 'Script1')
        self.AppendItem(tk, 'Script2')
        self.AppendItem(tk, 'Script3')
        self.AppendItem(tk, 'Script4+')
        self.AppendItem(tk, 'Script5')

#---------------------------------------------------------------------------
        
class MyFrame(wx.Frame):
    """
    Our customized window class.
    """
    def __init__(self, parent, id, title):
        """
        Initialize our window.
        """
        wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(800, 600))

        #self.SetIcon(wx.Icon('./icons/wxwin.ico', wx.BITMAP_TYPE_ICO))

        #------------
        
        # Create a splitter window.
        self.splitter = wx.SplitterWindow(self, -1, style=wx.SP_LIVE_UPDATE)
        
        # Create the left panel.
        leftPanel = wx.Panel(self.splitter, -1)
        
        # Create our tree and put it into the left panel.
        self.tree = MyTree(leftPanel, 1,
                           wx.DefaultPosition,
                           wx.DefaultSize,
                           wx.TR_HIDE_ROOT |
                           wx.TR_HAS_BUTTONS)
        self.tree.SetBackgroundColour('#3DB2FF')

        # Bind the OnSelChanged method to the tree.
        self.tree.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnSelChanged, id=1)
        

        # Create the right panel.
        rightPanel = wx.Panel(self.splitter, -1, style=wx.SUNKEN_BORDER)
        rightPanel.SetBackgroundColour('#79B4B7')
        
        # Create a widget to display static text 
        # and store it in the right panel.
        self.display = wx.StaticText(rightPanel, -1, '', style=wx.ALIGN_CENTER)
        #item =  self.display.SetLabel(self.tree.GetItemText())
        #print(item)
        
        # Put the left and right panes into the split window.
        self.splitter.SplitVertically(leftPanel, rightPanel, 200)

        # Minimum size of subwindow.
        self.splitter.SetMinimumPaneSize(1)         

        #------------

        # Create a box sizer that will contain the left panel contents.
        leftBox = wx.BoxSizer(wx.VERTICAL)
        
        # Add the tree to the box sizer.
        leftBox.Add(self.tree, 1, wx.EXPAND)
        
        # Set the size of the right panel to that required by the tree.
        leftPanel.SetSizer(leftBox)
        
        # Create the right box sizer that will contain the panel's contents.
        rightBox = wx.BoxSizer(wx.VERTICAL)
        
        # Add the display widget to the right panel.
        rightBox.Add(self.display, 0, wx.ALL, 1)


        self.button = wx.Button(rightPanel, wx.ID_ANY, 'Test', pos=(225, 2000))
        rightBox.Add(self.button)
        self.button.Bind(wx.EVT_BUTTON, onButton)
        print(self.button.GetScreenPosition())

        # Set the size of the right panel to that 
        # required by the display widget.
        rightPanel.SetSizer(rightBox)
        #------------
        
        # Create the window in the centre of the screen.
        self.Centre()

    #-----------------------------------------------------------------------
        
    def OnSelChanged(self, event):
        """
        Method called when selected item is changed.
        """
        
        # Get the selected item object.
        item =  event.GetItem()

        overview = """
        This demo simply catches all key events and prints info about them.
        It is meant to be used as a compatibility test for cross platform work.
        """
        
        # Display the selected item text in the text widget.
        if self.tree.GetItemText(item) == ' Big Script':
            self.display.SetLabel(f"{self.tree.GetItemText(item)}\n {overview}")
        else: 
            self.display.SetLabel(self.tree.GetItemText(item))
        
        return item

        #print(self.tree.GetItemText(item))

#---------------------------------------------------------------------------
        
class MyApp(wx.App):
    """
    Our application class.
    """
    def OnInit(self):
        """
        Initialize by creating the split window with the tree.
        """
        
        frame = MyFrame(None, -1, 'Test1')
        frame.Show(True)
        self.SetTopWindow(frame)
        
        return True

#---------------------------------------------------------------------------
    
if __name__ == '__main__':
    app = MyApp(0)
    app.MainLoop()

最后一个问题是,除了文档之外,您是否知道更深入地了解这个库的其他资源?(Youtube视频、Udemy等)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-09-22 08:35:46

有几件事要提:

在使用sizer时使用position语句是没有意义的,它将被忽略。

sizer类型决定位置。你已经选择了一个立式箱体,所以所有的东西都会垂直排列。您可以在添加小部件时使用对齐样式,或者选择不同的sizer类型,更适合您的需求。

我在类中移动了按钮的回调函数。

如果不依赖于事件值并发送虚拟事件参数(通常是None ),则可以单独调用按钮回调函数。

代码语言:javascript
复制
import wx
from wx.core import Font, Position, Size
import wx.lib.newevent
from wx.lib.agw.customtreectrl import CustomTreeCtrl

#---------------------------------------------------------------------------

class MyTree(wx.TreeCtrl):
    """
    Our customized TreeCtrl class.
    """
    def __init__(self, parent, id, position, size, style):
        """
        Initialize our tree.
        """
        wx.TreeCtrl.__init__(self, parent, id, position, size, style)
       
        root = self.AddRoot('Programmer')
        
        os = self.AppendItem(root, ' Big Script')
        pl = self.AppendItem(root, ' Big Script2')
        tk = self.AppendItem(root, 'Big Script3')
           
        cl = self.AppendItem(pl, 'Random Scripts')
        sl = self.AppendItem(pl, 'Miscelanious')

        self.AppendItem(os, 'Script1')
        self.AppendItem(os, 'Script2')
        self.AppendItem(os, 'Script3')
        self.AppendItem(os, 'Script4')
        self.AppendItem(cl, 'Script1')
        self.AppendItem(cl, 'Script2')
        self.AppendItem(cl, 'Script3')
        self.AppendItem(sl, '1')
        self.AppendItem(sl, '2')
        self.AppendItem(sl, '3')
        self.AppendItem(sl, '4')
        self.AppendItem(tk, 'Script1')
        self.AppendItem(tk, 'Script2')
        self.AppendItem(tk, 'Script3')
        self.AppendItem(tk, 'Script4+')
        self.AppendItem(tk, 'Script5')

#---------------------------------------------------------------------------
        
class MyFrame(wx.Frame):
    """
    Our customized window class.
    """
    def __init__(self, parent, id, title):
        """
        Initialize our window.
        """
        wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(800, 600))

        #self.SetIcon(wx.Icon('./icons/wxwin.ico', wx.BITMAP_TYPE_ICO))

        #------------
        
        # Create a splitter window.
        self.splitter = wx.SplitterWindow(self, -1, style=wx.SP_LIVE_UPDATE)
        
        # Create the left panel.
        leftPanel = wx.Panel(self.splitter, -1)
        
        # Create our tree and put it into the left panel.
        self.tree = MyTree(leftPanel, 1,
                           wx.DefaultPosition,
                           wx.DefaultSize,
                           wx.TR_HIDE_ROOT |
                           wx.TR_HAS_BUTTONS)
        self.tree.SetBackgroundColour('#3DB2FF')

        # Bind the OnSelChanged method to the tree.
        self.tree.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnSelChanged, id=1)
        

        # Create the right panel.
        rightPanel = wx.Panel(self.splitter, -1, style=wx.SUNKEN_BORDER)
        rightPanel.SetBackgroundColour('#79B4B7')
        
        # Create a widget to display static text 
        # and store it in the right panel.
        self.display = wx.StaticText(rightPanel, -1, '', style=wx.ALIGN_CENTER)
        #item =  self.display.SetLabel(self.tree.GetItemText())
        #print(item)
        
        # Put the left and right panes into the split window.
        self.splitter.SplitVertically(leftPanel, rightPanel, 200)

        # Minimum size of subwindow.
        self.splitter.SetMinimumPaneSize(1)         

        #------------

        # Create a box sizer that will contain the left panel contents.
        leftBox = wx.BoxSizer(wx.VERTICAL)
        
        # Add the tree to the box sizer.
        leftBox.Add(self.tree, 1, wx.EXPAND)
        
        # Set the size of the right panel to that required by the tree.
        leftPanel.SetSizer(leftBox)
        
        # Create the right box sizer that will contain the panel's contents.
        rightBox = wx.BoxSizer(wx.VERTICAL)
        
        # Add the display widget to the right panel.
        rightBox.Add(self.display, 0, wx.ALL, 1)


        #self.button = wx.Button(rightPanel, wx.ID_ANY, 'Test', pos=(225, 2000))
        # You're using a Sizer to arrange the contents of rightPanel so "position" is meaningless
        self.button = wx.Button(rightPanel, wx.ID_ANY, 'Test')
        # Use an alignment parameter or choose a sizer other than a boxsizer
        rightBox.Add(self.button,0,wx.ALIGN_RIGHT|wx.ALL, 20)
        self.button.Bind(wx.EVT_BUTTON, self.onButton)

        # Set the size of the right panel to that 
        # required by the display widget.
        rightPanel.SetSizer(rightBox)
        #------------
        
        # Create the window in the centre of the screen.
        self.Centre()

    #-----------------------------------------------------------------------
    def onButton(self, event):
        print("Button Pressed")
        
    def OnSelChanged(self, event):
        """
        Method called when selected item is changed.
        """
        
        # Get the selected item object.
        item =  event.GetItem()

        overview = """
        This demo simply catches all key events and prints info about them.
        It is meant to be used as a compatibility test for cross platform work.
        """
        
        # Display the selected item text in the text widget.
        if self.tree.GetItemText(item) == ' Big Script':
            self.display.SetLabel(f"{self.tree.GetItemText(item)}\n {overview}")
            # We can call the event function, as long as we set a value of something for the event parameter
            self.onButton(None)
        else: 
            self.display.SetLabel(self.tree.GetItemText(item))
        
#---------------------------------------------------------------------------
        
class MyApp(wx.App):
    """
    Our application class.
    """
    def OnInit(self):
        """
        Initialize by creating the split window with the tree.
        """
        
        frame = MyFrame(None, -1, 'Test1')
        frame.Show(True)
        self.SetTopWindow(frame)
        
        return True

#---------------------------------------------------------------------------
    
if __name__ == '__main__':
    app = MyApp()
    app.MainLoop()

票数 1
EN

Stack Overflow用户

发布于 2021-09-22 01:33:43

首先,是www.wxpython.org。然后是discuss.wxpython.org。但是真正的学习方法是为您的平台找到演示,它可以很容易地使用您可以窃取的代码浏览示例。转到www.wxpython.org并向下滚动到“额外文件”部分以找到演示。至于按钮位置问题,在wxpython中控制事物位置的最好方法是使用sizer。有关示例,请参见演示。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69276871

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档