gpt4 book ai didi

wxPython 中的 Python GUI 程序无法运行

转载 作者:太空宇宙 更新时间:2023-11-04 08:46:24 25 4
gpt4 key购买 nike

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

(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'

最佳答案

这只是一个错字。正确的方法是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)
...

希望这对您有所帮助!

关于wxPython 中的 Python GUI 程序无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40109565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com