python-3.x - wxPython:如何使用多个窗口?
问题描述
我正在制作一个程序,为您提供可供选择的艺术家列表,当您单击艺术家时,它会打开一个新窗口并为您提供他们的画作选择。但是,当我尝试这样做时,新窗口上没有任何反应。我不能在上面放任何按钮,甚至静态文本也不起作用,它在父窗口上工作得很好,但在子窗口上却不行。我错过了什么还是我需要完全做其他事情?这是我的代码:
import webbrowser
import sqlite3
import wx
conn = sqlite3.connect("dbPaintings.db")
c = conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS tblPaintings(Artist TEXT, PaintingName TEXT, Link TEXT)")
path = 'Artists Paintings.txt'
def insertingPaintings():
line = ''
filePaintings=open(path,'r')
for x in range(15):
arrPaintings = filePaintings.readline().split(",")
if len(arrPaintings) == 3:
strArtist = arrPaintings[0]
strPainting = arrPaintings[1]
strLink = arrPaintings[2]
strLink = strLink.split("\\")[0]
c.execute("INSERT INTO tblPaintings(Artist, PaintingName, Link) VALUES(:ArtistName, :Painting, :Url)", {'ArtistName':strArtist,'Painting':strPainting,'Url':strLink})
conn.commit()
#insertingPaintings()
class ArtFrame(wx.Frame):
def __init__(self, keyID, title, parent):
wx.Frame.__init__(self,parent=parent,title=title,size=(800,600))
panel = wx.Panel(self)
wx.StaticText(panel,-1,"Working",(566,345))
self.SetBackgroundColour('white')
if keyID=='Auguste Renoir':
self.AugusteRenoirWork()
def AugusteRenoirWork(self):
UmbrellasButtonImage = wx.Image('The Umbrellas.jpg',wx.BITMAP_TYPE_JPEG).ConvertToBitmap()
UmbrellasButton = wx.BitmapButton(wx.Panel(self),-1,UmbrellasButtonImage,pos=(200,200),size=(200,250))
class MainFrame(wx.Frame):
def __init__(self,*args,**kwargs):
super(MainFrame, self).__init__(*args, **kwargs, size=(1280,720), style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER ^ wx.MAXIMIZE_BOX)
self.basicGUI()
self.Centre()
def basicGUI(self):
panel = wx.Panel(self)
self.SetBackgroundColour('black')
self.SetTitle('French Impressionism')
font=wx.Font(18, wx.DECORATIVE, wx.ITALIC, wx.NORMAL)
AugusteRenoirButtonImage = wx.Image('Auguste Renoir.jpg',wx.BITMAP_TYPE_JPEG).ConvertToBitmap()
AugusteRenoirText = wx.StaticText(panel,-1,"Auguste Renoir",(237,345))
AugusteRenoirText.SetForegroundColour("#e6b800")
AugusteRenoirText.SetFont(font)
AugusteRenoirButton = wx.BitmapButton(panel,-1,AugusteRenoirButtonImage,pos=(220,85),size=(200,250))
self.Bind(wx.EVT_BUTTON,self.RenoirWindow,AugusteRenoirButton)
ClaudeMonetButtonImage = wx.Image('Claude Monet.jpg',wx.BITMAP_TYPE_JPEG).ConvertToBitmap()
ClaudeMonetText = wx.StaticText(panel,-1,"Claude Monet",(566,345))
ClaudeMonetText.SetForegroundColour("#206020")
ClaudeMonetText.SetFont(font)
ClaudeMonetButton = wx.BitmapButton(panel,-1,ClaudeMonetButtonImage,pos=(540,85),size=(200,250))
self.Bind(wx.EVT_BUTTON,self.MonetWindow,ClaudeMonetButton)
VincentVanGoghButtonImage = wx.Image('Vincent Van Gogh.jpg',wx.BITMAP_TYPE_JPEG).ConvertToBitmap()
VincentVanGoghText = wx.StaticText(panel,-1,"Vincent Van Gogh",(862,345))
VincentVanGoghText.SetForegroundColour("#3385ff")
VincentVanGoghText.SetFont(font)
VincentVanGoghButton = wx.BitmapButton(panel,-1,VincentVanGoghButtonImage,pos=(860,85),size=(200,250))
self.Bind(wx.EVT_BUTTON,self.VanGoghWindow,VincentVanGoghButton)
GeorgesSeuratButtonImage = wx.Image('Georges Seurat.jpg',wx.BITMAP_TYPE_JPEG).ConvertToBitmap()
GeorgesSeuratText = wx.StaticText(panel,-1,"Georges Seurat",(393,640))
GeorgesSeuratText.SetForegroundColour("#990099")
GeorgesSeuratText.SetFont(font)
GeorgesSeuratButton = wx.BitmapButton(panel,-1,GeorgesSeuratButtonImage,pos=(380,380),size=(200,250))
self.Bind(wx.EVT_BUTTON,self.SeuratWindow,GeorgesSeuratButton)
PaulCezanneButtonImage = wx.Image('Paul Cezanne.jpg',wx.BITMAP_TYPE_JPEG).ConvertToBitmap()
PaulCezanneText = wx.StaticText(panel,-1,"Paul Cezanne",(735,640))
PaulCezanneText.SetForegroundColour("#cc2900")
PaulCezanneText.SetFont(font)
PaulCezanneButton = wx.BitmapButton(panel,-1,PaulCezanneButtonImage,pos=(710,380),size=(200,250))
self.Bind(wx.EVT_BUTTON,self.CezanneWindow,PaulCezanneButton)
self.Show(True)
def RenoirWindow(self,event):
wx.Panel(self)
title = "Auguste Renoir's Works"
keyID='Auguste Renoir'
frame = ArtFrame(parent=wx.GetTopLevelParent(self),title=title,keyID=keyID)
frame.Show(True)
def MonetWindow(self,event):
wx.Panel(self)
title = "Claude Monet's Works"
keyID='Claude Monet'
frame = ArtFrame(parent=wx.GetTopLevelParent(self),title=title,keyID=keyID)
def VanGoghWindow(self,event):
wx.Panel(self)
title = "Vincent Van Gogh's Works"
keyID='Vincent Van Gogh'
frame = ArtFrame(parent=wx.GetTopLevelParent(self),title=title,keyID=keyID)
def SeuratWindow(self,event):
wx.Panel(self)
title = "Georges Seurat's Works"
keyID='Georges Seurat'
frame = ArtFrame(parent=wx.GetTopLevelParent(self),title=title,keyID=keyID)
def CezanneWindow(self,event):
wx.Panel(self)
title = "Paul Cezanne's Works"
keyID='Paul Cezanne'
frame = ArtFrame(parent=wx.GetTopLevelParent(self),title=title,keyID=keyID)
app = wx.App()
MainFrame(None)
app.MainLoop()
解决方案
请注意,如果您发布了一小段代码,任何人都可以运行,而无需您的数据库和 jpg 等,那会更容易
在
class ArtFrame(wx.Frame):
def __init__(self, keyID, title, parent):
wx.Frame.__init__(self,parent=parent,title=title,size=(800,600))
panel = wx.Panel(self)
wx.StaticText(panel,-1,"Working",(566,345))
self.SetBackgroundColour('white')
if keyID=='Auguste Renoir':
self.AugusteRenoirWork()
def AugusteRenoirWork(self):
UmbrellasButtonImage = wx.Image('The Umbrellas.jpg',wx.BITMAP_TYPE_JPEG).ConvertToBitmap()
UmbrellasButton = wx.BitmapButton(wx.Panel(self),-1,UmbrellasButtonImage,pos=(200,200),size=(200,250))
创建 wx.BitmapButton 时,它有一个新面板作为其父 wx.Panel(self),而不是使用在init中创建的面板
像这样的东西
class ArtFrame(wx.Frame):
def __init__(self, keyID, title, parent):
wx.Frame.__init__(self,parent=parent,title=title,size=(800,600))
self.panel = wx.Panel(self)
wx.StaticText(self.panel,-1,"Working",(566,345))
self.SetBackgroundColour('white')
if keyID=='Auguste Renoir':
self.AugusteRenoirWork()
def AugusteRenoirWork(self):
UmbrellasButtonImage = wx.Image('The Umbrellas.jpg',wx.BITMAP_TYPE_JPEG).ConvertToBitmap()
UmbrellasButton = wx.BitmapButton(self.panel,-1,UmbrellasButtonImage,pos=(200,200),size=(200,250))
推荐阅读
- python - 如何在数据框中就地替换1列中的数字?
- android - Android - 位图缩放 [QS 面板背景]
- python-3.x - 如何在 antlr4 中仅查找被覆盖的方法
- python - Keras predict_on_batch 和 predict 相当慢
- android - 如何对 Flowable.interval 进行单元测试?
- sql - db.Query with sql join error 列上的扫描错误
- ios - 从 iCloud 上传文件不起作用 Alamofire Multipart
- apache - nginx limit_conn_zone 在我的服务器中不起作用
- vsto - MS Word 的 VSTO 加载项一直自动被禁用
- c# - Xamarin:Microsoft 登录的 iOS 身份验证错误