2013-08-12 117 views
11

Đối với một built-in thoại như QInputDialog, tôi đã đọc mà tôi có thể làm điều này:Làm thế nào tôi có thể hiển thị hộp thoại kiểu PyQt và lấy dữ liệu ra khỏi các điều khiển khi đã đóng?

text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog', 'Enter your name:') 

Làm thế nào tôi có thể bắt chước hành vi này sử dụng một hộp thoại mà tôi thiết kế bản thân mình trong Qt Designer? Ví dụ, tôi muốn làm:

my_date, my_time, ok = MyCustomDateTimeDialog.get_date_time(self) 

Trả lời

21

Đây là lớp học đơn giản, bạn có thể sử dụng để nhắc nhở đối với ngày:

class DateDialog(QDialog): 
    def __init__(self, parent = None): 
     super(DateDialog, self).__init__(parent) 

     layout = QVBoxLayout(self) 

     # nice widget for editing the date 
     self.datetime = QDateTimeEdit(self) 
     self.datetime.setCalendarPopup(True) 
     self.datetime.setDateTime(QDateTime.currentDateTime()) 
     layout.addWidget(self.datetime) 

     # OK and Cancel buttons 
     buttons = QDialogButtonBox(
      QDialogButtonBox.Ok | QDialogButtonBox.Cancel, 
      Qt.Horizontal, self) 
     buttons.accepted.connect(self.accept) 
     buttons.rejected.connect(self.reject) 
     layout.addWidget(buttons) 

    # get current date and time from the dialog 
    def dateTime(self): 
     return self.datetime.dateTime() 

    # static method to create the dialog and return (date, time, accepted) 
    @staticmethod 
    def getDateTime(parent = None): 
     dialog = DateDialog(parent) 
     result = dialog.exec_() 
     date = dialog.dateTime() 
     return (date.date(), date.time(), result == QDialog.Accepted) 

và sử dụng nó:

date, time, ok = DateDialog.getDateTime() 
+0

bạn có thể giải thích lý do tại sao bạn sử dụng '@ staticmethod' decorat hoặc, và tại sao bạn đã chọn để khởi tạo lớp 'DateDialog' trong định nghĩa lớp' DateDialog'? Tôi thấy điều này hơi khó hiểu. – wesanyer

+0

@wesanyer Đúng. Bạn có thể sử dụng hàm đơn giản để tạo hộp thoại và nhận kết quả tốt hơn (nếu bạn không cần sử dụng lại lớp hộp thoại). Ngoài ra, nó có thể trả về đối tượng 'date' và trả về' None' hoặc ném ngoại lệ nếu hộp thoại đã bị hủy (không chắc chắn giải pháp đúng trong Python) là gì. – hluk

+0

Đối với những người không biết, QDialog có thể được nhập khẩu như sau: 'từ PyQt4.QtGui nhập QDialog' (giả sử bạn đang sử dụng PyQt phiên bản 4) – MD004

25

Tôi cố gắng để chỉnh sửa câu trả lời của hluk với những thay đổi bên dưới nhưng nó đã bị từ chối, không chắc chắn lý do tại sao bởi vì nó có một số lỗi rõ ràng như xa là tôi có thể nhìn thấy.

lỗi 1: đã xóa tự. từ self.layout.addWidget (self.buttons)

bugfix 2: kết nối nút OK và Cancel để hành động chính xác của nó

nâng cao: thực hiện các mã sẵn sàng để chạy bằng cách bao gồm nhập khẩu và cải thiện thời gian Ví dụ

from PyQt4.QtGui import QDialog, QVBoxLayout, QDialogButtonBox, QDateTimeEdit, QApplication 
from PyQt4.QtCore import Qt, QDateTime 

class DateDialog(QDialog): 
    def __init__(self, parent = None): 
     super(DateDialog, self).__init__(parent) 

     layout = QVBoxLayout(self) 

     # nice widget for editing the date 
     self.datetime = QDateTimeEdit(self) 
     self.datetime.setCalendarPopup(True) 
     self.datetime.setDateTime(QDateTime.currentDateTime()) 
     layout.addWidget(self.datetime) 

     # OK and Cancel buttons 
     self.buttons = QDialogButtonBox(
      QDialogButtonBox.Ok | QDialogButtonBox.Cancel, 
      Qt.Horizontal, self) 
     layout.addWidget(self.buttons) 

     self.buttons.accepted.connect(self.accept) 
     self.buttons.rejected.connect(self.reject) 

    # get current date and time from the dialog 
    def dateTime(self): 
     return self.datetime.dateTime() 

    # static method to create the dialog and return (date, time, accepted) 
    @staticmethod 
    def getDateTime(parent = None): 
     dialog = DateDialog(parent) 
     result = dialog.exec_() 
     date = dialog.dateTime() 
     return (date.date(), date.time(), result == QDialog.Accepted) 

và sử dụng nó:

app = QApplication([]) 
date, time, ok = DateDialog.getDateTime() 
print("{} {} {}".format(date, time, ok)) 
app.exec_()