Help changing someone else's code (default window size)
-
First, I must tell you that I am completely new to both Python and QT.
I recently built a book scanning device according to these instructions. The designer also made image capture software just for this device, which is fine, but they wrote the code on the assumption that a large monitor would be used. Without realizing this, I purchased a 7" Capacitive Touch Screen For Raspberry Pi (1024*600), and when I ran the program, only the upper left corner was visible. When I resize the window, the text and buttons become illegible and unusable. I figured out that the program (which can be found here) uses QT, and furthermore I think I tracked down the culprit to this bit of code in the main file (SimpleBookCapture dot py):# メインWindow初期設定 self.resize(int(1920*3/4), int(1080*3/4)) self.move(100, 100) self.setWindowTitle('Simple Book Capture Ver.0.0.1') self.show()(The Japanese means "Main Window Initial Settings.") I believe there is some way to replace those numbers with percentages of available screen, such as "0.9."
Then there remains the problem of tweaking how other things get scaled, so that they are legible and useable on my little monitor, but I haven't looked for that code yet.
Here's a video of me launching the program and resizing the screen. Although the text on the screen is mostly Japanese, you can see by looking at the numbers that the top and bottom of all text gets chopped off a bit, so that "100" looks like "|( )( )."
This scanner is made to be used at the Kyoto International Manga Museum, and will be used by a number of people besides me. I can't expect them all to learn how to resize the window (which is awkward) and make sense of the cramped results.
Any guidance would be very much appreciated. -
Hi,
I'm not a python guy, and you didn't tell us what bindings you're using, but I know generally what to look for/turn to in Qt:
So here's a ChatGPT example. You get the basic premise, but no guarantee this example is actually compiling and running :Pfrom PyQt6.QtWidgets import QApplication, QMainWindow, QLabel from PyQt6.QtGui import QScreen, QFont class MainWindow(QMainWindow): def __init__(self): super().__init__() screen = QApplication.primaryScreen() screen_size = screen.size() screen_width = screen_size.width() screen_height = screen_size.height() self.resize(int(screen_width * 3 / 4), int(screen_height * 3 / 4)) self.move((screen_width - self.width()) // 2, (screen_height - self.height()) // 2) font_size = int(screen_height * 0.02) font = QFont("Arial", font_size) self.setFont(font) label = QLabel("Welcome to Simple Book Capture!", self) label.setFont(font) label.move(50, 50) self.setWindowTitle('Simple Book Capture Ver.0.0.1') self.show() app = QApplication([]) window = MainWindow() app.exec() -
First, I must tell you that I am completely new to both Python and QT.
I recently built a book scanning device according to these instructions. The designer also made image capture software just for this device, which is fine, but they wrote the code on the assumption that a large monitor would be used. Without realizing this, I purchased a 7" Capacitive Touch Screen For Raspberry Pi (1024*600), and when I ran the program, only the upper left corner was visible. When I resize the window, the text and buttons become illegible and unusable. I figured out that the program (which can be found here) uses QT, and furthermore I think I tracked down the culprit to this bit of code in the main file (SimpleBookCapture dot py):# メインWindow初期設定 self.resize(int(1920*3/4), int(1080*3/4)) self.move(100, 100) self.setWindowTitle('Simple Book Capture Ver.0.0.1') self.show()(The Japanese means "Main Window Initial Settings.") I believe there is some way to replace those numbers with percentages of available screen, such as "0.9."
Then there remains the problem of tweaking how other things get scaled, so that they are legible and useable on my little monitor, but I haven't looked for that code yet.
Here's a video of me launching the program and resizing the screen. Although the text on the screen is mostly Japanese, you can see by looking at the numbers that the top and bottom of all text gets chopped off a bit, so that "100" looks like "|( )( )."
This scanner is made to be used at the Kyoto International Manga Museum, and will be used by a number of people besides me. I can't expect them all to learn how to resize the window (which is awkward) and make sense of the cramped results.
Any guidance would be very much appreciated.Hi and welcome to the forum :)
I've read bits of the original code and couldn't find anything suspicious.
So even when you resize the window manually, the result is not satisfying or what you expect?
In your video it looks like some comboBoxes (the elements the with a drop-down menu) are not displayed correctly?!@Rachel_T said in Help changing someone else's code (default window size):
I believe there is some way to replace those numbers with percentages of available screen, such as "0.9."
Then there remains the problem of tweaking how other things get scaled, so that they are legible and useable on my little monitorYes it should be possible, in fact the actual code already uses some kind of scaling, because as you can see
self.resize(int( 1920*3/4 ), int( 1080*3/4) )it uses 3/4 of 1920x1080 (which equals the size of FullHD resolution). I assume it's due to some scaling of their original screen.
Ah, while I am typing this, I see @J-Hilk has replied.
You can use code like he posted above to get your current screen size
(This part here)
screen = QApplication.primaryScreen() screen_size = screen.size() screen_width = screen_size.width() screen_height = screen_size.height() # this does not need to be 3/4... # play around with different sizes until you get what you want self.resize(int(screen_width * 3 / 4), int(screen_height * 3 / 4)) # this is where the window appears on your screen. # topleft corner is the origin of the coordinate system ( 0 / 0 ) self.move((screen_width - self.width()) / 2, (screen_height - self.height()) / 2)or you try one of
self.showFullScreen()or
self.showMaximized()instead of just
self.show()But I don't know for sure whether it will work (and set the correct size) on PyQt and your Raspberry.
Edit:
@J-Hilk The original code says PyQt5... Can't tell if there are possible issues with scaling and screen sizes (:
Could not find any trace of something aroundHighDPIScalingor so... -
There are items in this main window that I don't need and can't use anyway, and I found those components in a script named "CameraSettingsPage dot py."
I was able to delete the "Save RAW and Meta data" item easily, so I saved a whole line of space there, but when I deleted code for the "Sensor mode" (which also seems to be unusable)...# センサモードのコンボボックス self.sensorFormat = QComboBox() #self.sensorFormat.addItem("Default") self.sensorFormat.addItems([f'{x["formatfsensor"].format} {x["size"]}' for x in picam2s[camid].sensor_modes]) # 最大の解像度(4608x2592)に設定 # プレビューとキャプチャの画角違い防止のため固定 self.sensorFormat.setCurrentIndex(2) self.sensorFormat.setEnabled(False) formLayout.addRow("センサーモード", self.sensorFormat)...the program would no longer launch. I assume there are references to things in this code elsewhere in this script (or maybe other scripts) that I would also need to delete or modify.
I also would like to get rid of the "Image Size" (画像サイズ) item just below the sensor mode item.# 画像サイズ反映ボタン self.resButton = QPushButton("反映") self.resButton.setEnabled(False) # 画像サイズ反映ボタンクリック時の動作 self.resButton.clicked.connect(self.on_resButton_clicked) # 画像の縦横サイズを変更時は反映ボタンを有効化 self.imageWidth.valueChanged.connect(lambda: self.resButton.setEnabled(True)) self.imageHeight.valueChanged.connect(lambda: self.resButton.setEnabled(True)) # 画像サイズ resolution = QWidget() resHLayout = QHBoxLayout() resolution.setLayout(resHLayout) resHLayout.addWidget(self.imageWidth) resHLayout.addWidget(QLabel("x"), alignment=Qt.AlignHCenter) resHLayout.addWidget(self.imageHeight) resHLayout.addWidget(QLabel(" "), alignment=Qt.AlignHCenter) resHLayout.addWidget(self.resButton) formLayout.addRow("画像サイズ", resolution)Getting rid of those two would make this window plenty usable and legible. (As it is, the "Rotation" control, just below the "Image size" control, is unusable with the window sized to fit my screen, because the button isn't visible.)