问题描述
如何打开一个允许我从以下代码中选择时间的新窗口?我尝试使用connect函数连接到windows2,但似乎出现错误.
how do i open a new window which allows me to select the time from the following code? i tried to use connect function to connect to windows2 however it appears that there is an error.
我想通过 dropbox 选择时间,我可以在上午 10 点、上午 11 点等之前选择时间.有谁知道您也可以如何实现这一点?
i would like to select time by a dropbox where i could choose time by 10 am, 11 am, ect.. does anyone know how you could implement this as well?
class window(qmainwindow): def __init__(self): super().__init__() self.label = qlabel() self.calendar = qcalendarwidget() self.title="select date from calendar" self.left = 600 self.top = 300 self.width = 500 self.height = 480 self.iconname = "home.png" self.setwindowtitle(self.title) self.setwindowicon(qtgui.qicon(self.iconname)) self.setgeometry(self.left, self.top, self.width, self.height) self.proceedbutton = qpushbutton("proceed to select time", self) self.proceedbutton.setgeometry(290, 430, 190, 40) self.proceedbutton.settooltip("start the session
") self.proceedbutton.clicked.connect(self.window2) self.hide() self.backbutton = qpushbutton("back", self) self.backbutton.setgeometry(200, 430, 80, 40) self.backbutton.settooltip("start the session
") self.calendar() self.show() def calendar(self): calendarvbox = qvboxlayout() self.calendar.setgridvisible(true) self.label.setfont(qtgui.qfont("sanserif", 10)) self.label.setstylesheet('color:black') calendarvbox.addwidget(self.calendar) calendarvbox.addwidget(self.label) self.setlayout(calendarvbox) self.calendar.selectionchanged.connect(self.onselectionchanged) def window2(self): self.label = qlabel("select time", self) self.label.move(200,430) self.setwindowtitle("select time") self.setgeometry(self.left, self.top, self.width, self.height) self.show() def onselectionchanged(self): ca = self.calendar.selecteddate() self.label.settext(ca.tostring()) app = qapplication(sys.argv) window = window() sys.exit(app.exec())
推荐答案
开始使用layouts!
没有父级的小部件 - 有一个窗口.
a widget without a parent - there is a window.
import sys from pyqt5 import qtcore, qtgui, qtwidgets from pyqt5.qtwidgets import * from pyqt5.qtcore import * from pyqt5.qtgui import * class window(qwidget): #(qmainwindow): def __init__(self): super().__init__() self.title="select date from calendar" self.left, self.top, self.width, self.height = 600, 100, 500, 480 self.iconname = "ok.png" # <--- home.png self.setwindowtitle(self.title) self.setwindowicon(qtgui.qicon(self.iconname)) self.setgeometry(self.left, self.top, self.width, self.height) self.calendar = qcalendarwidget() self.calendar.setgridvisible(true) self.calendar.selectionchanged.connect(self.onselectionchanged) self.label = qlabel() self.label.setfont(qtgui.qfont("sanserif", 10)) self.label.setstylesheet('color: blue;') self.proceedbutton = qpushbutton("proceed to select time", self) self.proceedbutton.settooltip("start the session
") self.proceedbutton.clicked.connect(self.window2) self.backbutton = qpushbutton("back", self) self.backbutton.settooltip("start the session
") self.combobox = none self.grid = qtwidgets.qgridlayout(self) self.grid.addwidget(self.calendar, 0, 0, 1, 3) self.grid.addwidget(self.label, 1, 0, 1, 3) self.grid.addwidget(self.backbutton, 2, 1, 1, 1) self.grid.addwidget(self.proceedbutton, 2, 2, 1, 1) def window2(self): self.window = qwidget() self.window.setwindowtitle("select time") self.window.setgeometry(self.left/3, self.top, self.width/3, self.height/3) self.label = qlabel("select time") # --- , self) self.combobox = qtwidgets.qcombobox() self.combobox.additems(["choose time", "10", "11", "12"]) self.combobox.activated[str].connect(self.oncomboactivated) layout = qformlayout(self.window) layout.addrow('choose time', self.combobox) self.window.show() def onselectionchanged(self): ca = self.calendar.selecteddate() self.label.settext(ca.tostring()) def oncomboactivated(self, text): print("choose time: {}".format(text)) if __name__ == '__main__': app = qapplication(sys.argv) window = window() window.show() sys.exit(app.exec())