wxPython中的Python GUI程序将无法运行

时间:2016-10-18 13:24:56

标签: python wxpython

我有以下代码,我正在学习教程:

http://zetcode.com/wxpython/layout/ - GoToClass部分)

我无法弄清楚它有什么问题......:/

正如您在教程中看到的,它应该产生这个:

enter image description here

代码:

#!/usr/bin/python
# -*- coding: utf-8 -*-

# gotoclass.py

import wx

class Example(wx.Frame):

    def __init__(self, parent, title):
        super(Example, self).__init__(parent, title=title, 
            size=(390, 350))

        self.InitUI()
        self.Centre()
        self.Show()     

    def InitUI(self):

        panel = wx.Panel(self)

        font = wx.SystemSettings_GetFont(wx.SYS_SYSTEM_FONT)
        font.SetPointSize(9)

        vbox = wx.BoxSizer(wx.VERTICAL)

        hbox1 = wx.BoxSizer(wx.HORIZONTAL)
        st1 = wx.StaticText(panel, label='Class Name')
        st1.SetFont(font)
        hbox1.Add(st1, flag=wx.RIGHT, border=8)
        tc = wx.TextCtrl(panel)
        hbox1.Add(tc, proportion=1)
        vbox.Add(hbox1, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)

        vbox.Add((-1, 10))

        hbox2 = wx.BoxSizer(wx.HORIZONTAL)
        st2 = wx.StaticText(panel, label='Matching Classes')
        st2.SetFont(font)
        hbox2.Add(st2)
        vbox.Add(hbox2, flag=wx.LEFT | wx.TOP, border=10)

        vbox.Add((-1, 10))

        hbox3 = wx.BoxSizer(wx.HORIZONTAL)
        tc2 = wx.TextCtrl(panel, style=wx.TE_MULTILINE)
        hbox3.Add(tc2, proportion=1, flag=wx.EXPAND)
        vbox.Add(hbox3, proportion=1, flag=wx.LEFT|wx.RIGHT|wx.EXPAND, 
            border=10)

        vbox.Add((-1, 25))

        hbox4 = wx.BoxSizer(wx.HORIZONTAL)
        cb1 = wx.CheckBox(panel, label='Case Sensitive')
        cb1.SetFont(font)
        hbox4.Add(cb1)
        cb2 = wx.CheckBox(panel, label='Nested Classes')
        cb2.SetFont(font)
        hbox4.Add(cb2, flag=wx.LEFT, border=10)
        cb3 = wx.CheckBox(panel, label='Non-Project classes')
        cb3.SetFont(font)
        hbox4.Add(cb3, flag=wx.LEFT, border=10)
        vbox.Add(hbox4, flag=wx.LEFT, border=10)

        vbox.Add((-1, 25))

        hbox5 = wx.BoxSizer(wx.HORIZONTAL)
        btn1 = wx.Button(panel, label='Ok', size=(70, 30))
        hbox5.Add(btn1)
        btn2 = wx.Button(panel, label='Close', size=(70, 30))
        hbox5.Add(btn2, flag=wx.LEFT|wx.BOTTOM, border=5)
        vbox.Add(hbox5, flag=wx.ALIGN_RIGHT|wx.RIGHT, border=10)

        panel.SetSizer(vbox)


if __name__ == '__main__':

    app = wx.App()
    Example(None, title='Go To Class')
    app.MainLoop()

以下是我得到的错误:(删除了文件的路径

Traceback (most recent call last):
  File "[path]", line 78, in <module>
    Example(None, title='Go To Class')
  File "[path]", line 14, in __init__
    self.InitUI()
  File "[path]", line 21, in InitUI
    font = wx.SystemSettings_GetFont(wx.SYS_SYSTEM_FONT)
AttributeError: 'module' object has no attribute 'SystemSettings_GetFont'

1 个答案:

答案 0 :(得分:5)

这只是一个错字。正确的方法是wx.SystemSettings.GetFont(),另请参阅文档:https://wxpython.org/Phoenix/docs/html/wx.SystemSettings.html#wx.SystemSettings.GetFont

InitUI方法更改为:

def InitUI(self):
    panel = wx.Panel(self)

    font = wx.SystemSettings.GetFont(wx.SYS_SYSTEM_FONT)
    font.SetPointSize(9)
    ...

希望这有帮助!