Tôi đang viết một ứng dụng PyQt mới. Tôi đang cố gắng làm tất cả mọi thứ liên quan đến chương trình và ui sử dụng càng nhiều các API PyQt càng tốt như một phương tiện để cải thiện kiến thức của tôi về PyQt và Qt nói chung.Phân tích đối số dòng lệnh thanh lịch cho PyQt
Câu hỏi tôi có là, có API trong PyQt/Qt để xử lý đối số dòng lệnh phân tích cú pháp một cách thanh lịch không?
Nghiên cứu của tôi cho đến nay đã bật lên:
- một ví dụ về làm thế nào để làm cho nó play nice with python's opt_parser mô-đun, ngoại trừ nó không xử lý QApplication được xây dựng trong arg phân tích cú pháp.
- PyKDE's KCmdLineArgs (trong đó giới thiệu một không mong muốn KDE phụ thuộc)
- nó trông giống như KCmdLineArgs đang được chuyển ngược dòng cho Qt5.1 như QCommandLineParser, đó là mát mẻ, nhưng tôi muốn để có thể sử dụng nó bây giờ, không 18 tháng từ giờ.
Vậy các ứng dụng PyQt thường xử lý việc này như thế nào? hoặc là opt_parser/argparse con đường để đi?
Đây là xa một giải pháp tốt đẹp ...
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys, argparse
from PyQt4 import QtGui
def main(argv):
app = QtGui.QApplication(argv) # QApplication eats argv in constructor
# We can get a QStringList out of QApplication of those arguments it
# didn't decide were reserved by Qt.
argv2 = app.arguments()
# now we need to turn them back into something that optparse/argparse
# can understand, since a QStringList is not what it wants
argv3 = []
for i in argv2:
argv3.append(str(i))
# now we can pass this to optparse/argparse
process_args(argv3)
# dummy app
mw = QtGui.QMainWindow()
mw.show()
sys.exit(app.exec_())
def process_args(argv):
parser = argparse.ArgumentParser(description='PyQt4 argstest',
add_help=False)
# we now have to add all of the options described at
# http://qt-project.org/doc/qt-4.8/qapplication.html#QApplication
# but have them do nothing - in order to have them show up in the help list
# add this to the list if Qt is a debug build (How to detect this?)
parser.add_argument("-nograb", action=ignore,
help="don't grab keyboard/mouse for debugging")
# add these to the list if Qt is a debug build for X11
parser.add_argument("-dograb", action=ignore,
help="grab keyboard/mouse for debugging")
parser.add_argument("-sync", action=ignore,
help="run in synchronous mode for debugging")
# add all the standard args that Qt will grab on all platforms
parser.add_argument("-reverse", action=ignore,
help="run program in Right-to-Left mode")
# an example -- there are 10 such items in the docs for QApplication
# then we need to figure out if we're running on X11 and add these
parser.add_argument("-name", action=ignore,
help="sets the application name")
# an example -- there are 13 such items in the docs
# reimplement help (which we disabled above) so that -help works rather
# than --help; done to be consistent with the style of args Qt wants
parser.add_argument("-h", "-help", action='help',
help="show this help message and exit")
parser.parse_args(argv[1:])
class ignore(argparse.Action):
# we create an action that does nothing, so the Qt args do nothing
def __call__(self, parser, namespace, values, option_string=None):
pass
if __name__ == "__main__":
main(sys.argv)
Toàn bộ điểm của bài đăng của tôi tại lateral.netmanagers.com.ar mà bạn đã đề cập là nó xử lý phân tích cú pháp dòng lệnh dựng sẵn của QApplication. Bạn xác định các đối số của riêng bạn bằng cách sử dụng opt_parse/argparse/bất cứ điều gì, và miễn là bạn không xác định những cái mà QApplication sử dụng, mọi thứ chỉ hoạt động. –