问题描述
我有一个名为test()"的方法,它将 3 个单行表加载到滚动条中.
i have a method named 'test()' that loads 3 one-row tables into a scrollbar.
由于某种原因,我无法弄清楚,但是,如果我只是在加载时激活 test() 它会起作用,但如果我将其注释掉然后尝试通过按下按钮来激活它,它就不起作用.
for some reason that i cannot figure out, however, the while it works if i simply activate test() on load, it doesn't work if i comment it out and then try to activate it via the push of a button.
这里是主模块(带有 test())
here is the main module (with test())
from pyqt5 import qtcore, qtwidgets from design import ui_mainwindow class mainwindow(qtwidgets.qmainwindow, ui_mainwindow): def __init__(self, *args, **kwargs): qtwidgets.qmainwindow.__init__(self, *args, **kwargs) self.setupui(self) #test(self) def test(self): from random import randint x = randint(0, 99) print(x) height = 30 ypos = 0 for i in range(3): rowvals = ['test%s' % str(x i)] qtbl = qtwidgets.qtablewidget(self.sawdoc) qtbl.setobjectname("tbl%s" % (i)) qtbl.setgeometry(qtcore.qrect(0, ypos, 880, height)) qtbl.horizontalheader().setvisible(false) qtbl.verticalheader().setvisible(false) ypos = height qtbl.setrowcount(1) qtbl.setcolumncount(len(rowvals)) for r, cell in enumerate(rowvals): item = qtwidgets.qtablewidgetitem() item.settext(str(cell)) qtbl.setitem(0, r, item) if __name__ == "__main__": import sys app = qtwidgets.qapplication(sys.argv) w = mainwindow() w.show() sys.exit(app.exec_())
这里是设计模块(带有按钮)
and here is the design module (with the button)
from pyqt5 import qtcore, qtgui, qtwidgets class ui_mainwindow(object): def setupui(self, mainwindow): mainwindow.setobjectname("mainwindow") mainwindow.resize(896, 453) self.centralwidget = qtwidgets.qwidget(mainwindow) self.centralwidget.setobjectname("centralwidget") self.scrdoc = qtwidgets.qscrollarea(self.centralwidget) self.scrdoc.setgeometry(qtcore.qrect(0, 0, 891, 391)) self.scrdoc.setverticalscrollbarpolicy(qtcore.qt.scrollbaralwayson) self.scrdoc.sethorizontalscrollbarpolicy(qtcore.qt.scrollbaralwaysoff) self.scrdoc.setwidgetresizable(false) self.scrdoc.setobjectname("scrtest") self.sawdoc = qtwidgets.qwidget() self.sawdoc.setgeometry(qtcore.qrect(0, 0, 869, 300)) self.sawdoc.setobjectname("sawdoc") self.scrdoc.setwidget(self.sawdoc) self.btntest = qtwidgets.qpushbutton(self.centralwidget) self.btntest.setgeometry(qtcore.qrect(430, 400, 80, 15)) self.btntest.setobjectname("btntest") mainwindow.setcentralwidget(self.centralwidget) self.statusbar = qtwidgets.qstatusbar(mainwindow) self.statusbar.setobjectname("statusbar") mainwindow.setstatusbar(self.statusbar) self.retranslateui(mainwindow) qtcore.qmetaobject.connectslotsbyname(mainwindow) self.btntest.clicked.connect(self.test) def test(self): import main main.test(self) def retranslateui(self, mainwindow): _translate = qtcore.qcoreapplication.translate mainwindow.setwindowtitle(_translate("mainwindow", "昊珩のcat工具")) self.btntest.settext(_translate("mainwindow", "test")) if __name__ == "__main__": import sys app = qtwidgets.qapplication(sys.argv) mainwindow = qtwidgets.qmainwindow() ui = ui_mainwindow() ui.setupui(mainwindow) mainwindow.show() sys.exit(app.exec_())
当我按下按钮时,什么也没有发生(除了打印成功).
simply nothing happens when i push the button (except for the print out working successfully).
谁能告诉我怎么了?
推荐答案
一开始是父母给孩子看,之后孩子自己负责展示自己,简单的百家乐凯发k8的解决方案是使用show():
the parents show the children at the beginning, but afterwards it is the responsibility of the children to show themselves, the simple solution is to use show():
qtbl = qtwidgets.qtablewidget(self.sawdoc) qtbl.show()
但是我看到你以一种不优雅的方式实现百家乐凯发k8的解决方案,连接在 main.py 中完成,不要修改 qt designer 生成的文件(删除 design.py 中的连接和测试方法)您必须使用布局并在其中添加 qtablewidget.
but i see that you are implementing the solution in an inelegant way, the connection do it in main.py, do not modify the file generated by qt designer (delete the connection and the test method in design.py) you must use a layout and there add the qtablewidget.
class mainwindow(qtwidgets.qmainwindow, ui_mainwindow): def __init__(self, *args, **kwargs): qtwidgets.qmainwindow.__init__(self, *args, **kwargs) self.setupui(self) self.btntest.clicked.connect(self.test) def test(self): from random import randint x = randint(0, 99) lay = qtwidgets.qvboxlayout(self.sawdoc) for i in range(3): rowvals = ['test%s' % str(x i)] qtbl = qtwidgets.qtablewidget() qtbl.setobjectname("tbl%s" % (i)) qtbl.horizontalheader().hide() qtbl.verticalheader().hide() qtbl.setrowcount(1) qtbl.setcolumncount(len(rowvals)) for r, cell in enumerate(rowvals): item = qtwidgets.qtablewidgetitem() item.settext(str(cell)) qtbl.setitem(0, r, item) qtbl.resize(qtbl.sizehint()) lay.addwidget(qtbl)