[問題] pyqt 滑鼠點擊事件創造按鈕 無法顯示

看板Python作者 (QQ)時間3年前 (2020/07/23 02:39), 3年前編輯推噓2(203)
留言5則, 2人參與, 3年前最新討論串1/1
首先來個簡單的例子, 要在MainWindow上貼一個pushbutton: from PyQt5.QtWidgets import* w = QMainWindow() QPushButton("Hi",w) w.show() 這樣run起來確實就有一個"Hi"的按鈕 再來是用class來創造的例子: class MainWindow(QMainWindow): def __init__(self,*args): super().__init__(*args) def create_btn(self): QPushButton("Hi",self) w = MainWindow() w.create_btn() w.show() 這樣run起來也是成功的! 可是今天我如果用"mousePressEvent"去創造的話: class MainWindow(QMainWindow): def __init__(self,*args): super().__init__(*args) def mousePressEvent(self,e): QPushButton("Hi",self) w = MainWindow() w.show() 這樣run起來的話, 一開始當然沒有button, 可是我點擊滑鼠後還是沒有show出button 即便我多寫了"self.repaint()"仍是看不到 ("QPushButton("Hi",self)"如果擺在__init__裡面當然會成功, 不討論) 而因為我IDE是spyder, 所以run結束後可以繼續單步執行 因此我去print "w.children()" 發現button確實已經加入w了 以結果論來說, 我猜測是mousePressEvent所創出的child沒有跟parent做連結 但是不知道所謂的"連結"到底在哪 想請教一下問題出在哪呢??? 謝謝解惑! -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.110.132.77 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1595443186.A.ED1.html ※ 編輯: znmkhxrw (123.110.132.77 臺灣), 07/23/2020 03:11:32 ※ 編輯: znmkhxrw (42.72.62.61 臺灣), 07/23/2020 12:58:57

07/24 12:56, 3年前 , 1F
請問你是在 mousePressEvent裡加上 "*.show()"解決的
07/24 12:56, 1F

07/24 12:56, 3年前 , 2F
嗎?
07/24 12:56, 2F

07/27 22:50, 3年前 , 3F
嘿對 我關鍵字下錯 朋友找到的reference說在parent.show
07/27 22:50, 3F

07/27 22:50, 3年前 , 4F
後所加的object的default都是isHidden()=True
07/27 22:50, 4F

07/28 18:03, 3年前 , 5F
OK,謝謝
07/28 18:03, 5F
文章代碼(AID): #1V68VoxH (Python)