QApplication.primaryScreen()
时间: 2024-06-17 12:03:52 浏览: 232
QApplication.primaryScreen() 是一个Qt库中的函数,用于获取主屏幕的指针。主屏幕是指当前系统默认的主屏幕,通常为用户桌面上的屏幕。
可以使用primaryScreen()函数获取主屏幕的指针,然后调用其他Qt库中的函数来操作或显示图形界面。例如,可以使用QScreen类的geometry()函数获取主屏幕的宽度和高度等信息。
注意:在多屏幕环境下,primaryScreen()返回的是当前系统默认的主屏幕,而不一定是用户当前所在窗口所在的屏幕。
相关问题
app = QApplication(sys.argv) screen = QApplication.primaryScreen() def jietu(self, hwnd): img = self.screen.grabWindow(hwnd).toImage() buffer = QBuffer() buffer.open(QBuffer.ReadWrite) img.save(buffer,"bmp") dd = Image.open(buffer) #dd.save("1.bmp") return dd def remove_colors_except(image, target_colors, threshold): result = np.zeros_like(image) for target_color in target_colors: #例如 target_colors = ['FF0000', '00FF00', '0000FF'] # 将目标色格式 "FFFFFF" 转换成 RGB 值 blue = int(target_color[0:2], 16) green = int(target_color[2:4], 16) red = int(target_color[4:6], 16) # 计算目标颜色的范围 lower_range = np.array([blue - threshold, green - threshold, red - threshold]) upper_range = np.array([blue + threshold, green + threshold, red + threshold]) # 创建掩码,将目标颜色范围以外的像素设为黑色,目标颜色范围内的像素设为白色 mask = cv2.inRange(image, lower_range, upper_range) # 将目标颜色范围内的像素设为白色 result[mask > 0] = (255, 255, 255) # 显示结果图像 cv2.imwrite('222.bmp', result) return result ff=fangfa() def main(): hwnd=ff.huoqujubin('【魔域】') if hwnd!=[]: jietushuju=ff.jietu(hwnd) ff.remove_colors_except(jietushuju, ['FFFFFF'], 0) 报错 ff.remove_colors_except(jietushuju, ['FFFFFF'], 0) TypeError: fangfa.remove_colors_except() takes 3 positional arguments but 4 were given
根据你提供的代码,`remove_colors_except` 方法应该是 `fangfa` 类的一个方法,因此在调用时应该使用 `self.remove_colors_except` 而不是 `ff.remove_colors_except`。修改代码如下:
```python
app = QApplication(sys.argv)
screen = QApplication.primaryScreen()
class fangfa:
def jietu(self, hwnd):
img = screen.grabWindow(hwnd).toImage()
buffer = QBuffer()
buffer.open(QBuffer.ReadWrite)
img.save(buffer,"bmp")
dd = Image.open(buffer)
#dd.save("1.bmp")
return dd
def remove_colors_except(self, image, target_colors, threshold):
result = np.zeros_like(image)
for target_color in target_colors:
#例如 target_colors = ['FF0000', '00FF00', '0000FF']
# 将目标色格式 "FFFFFF" 转换成 RGB 值
blue = int(target_color[0:2], 16)
green = int(target_color[2:4], 16)
red = int(target_color[4:6], 16)
# 计算目标颜色的范围
lower_range = np.array([blue - threshold, green - threshold, red - threshold])
upper_range = np.array([blue + threshold, green + threshold, red + threshold])
# 创建掩码,将目标颜色范围以外的像素设为黑色,目标颜色范围内的像素设为白色
mask = cv2.inRange(image, lower_range, upper_range)
# 将目标颜色范围内的像素设为白色
result[mask > 0] = (255, 255, 255)
# 显示结果图像
cv2.imwrite('222.bmp', result)
return result
ff = fangfa()
def main():
hwnd = ff.huoqujubin('【魔域】')
if hwnd != []:
jietushuju = ff.jietu(hwnd)
ff.remove_colors_except(jietushuju, ['FFFFFF'], 0)
```
另外,如果你的代码中有 `import` 语句,需要将其也放在代码开头的位置。
def initUI(self): self.setWindowTitle(self.title) screen = QApplication.primaryScreen() size = screen.size() self.setGeometry((size.width() - self.width) // 2, (size.height() - self.height) // 2, self.width, self.height) # Prompt Label and Edit box prompt_label = QLabel('对象:', self) prompt_label.setStyleSheet('color: #222; font-size: 30px; margin-bottom: 10px; font-weight: bold;') self.prompt_edit = QTextEdit(self) self.prompt_edit.setStyleSheet('color: #333; font-size: 24px; border: 1px solid #ccc; padding: 5px;') prompt_layout = QHBoxLayout() prompt_layout.addWidget(prompt_label) prompt_layout.addWidget(self.prompt_edit) # Question Label and Edit box question_label = QLabel('问题:', self) question_label.setStyleSheet('color: #222; font-size: 30px; margin-bottom: 10px; font-weight: bold;') self.question_edit = QTextEdit(self) self.question_edit.setStyleSheet('color: #333; font-size: 24px; border: 1px solid #ccc; padding: 5px;') question_layout = QHBoxLayout() question_layout.addWidget(question_label) question_layout.addWidget(self.question_edit) liangge_layout_layout = QVBoxLayout() liangge_layout_layout.addLayout(prompt_layout) liangge_layout_layout.addLayout(question_layout) # Adding submit button to question layout self.submit_btn = QPushButton('发送', self) self.submit_btn.setFixedWidth(150) self.submit_btn.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Expanding) self.submit_btn.setStyleSheet('color: #fff; background-color: #20639b; border: none; font-size: 24px; padding: 10px; border-radius: 5px;') self.submit_btn.clicked.connect(self.on_submit) # Change layout of question and submit button using QHBoxLayout question_submit_layout = QHBoxLayout() question_submit_layout.addLayout(liangge_layout_layout) question_submit_layout.addWidget(self.submit_btn) # JSON response Label and Edit box json_response_label = QLabel('回复:', self) json_response_label.setStyleSheet('color: #222; font-size: 30px; margin-bottom: 10px; font-weight: bold;') self.json_response_edit = QTextEdit(self) self.json_response_edit.setStyleSheet('color: #333; font-size: 24px; border: 1px solid #ccc; padding: 5px;') json_response_layout = QHBoxLayout() json_response_layout.addWidget(json_response_label) json_response_layout.addWidget(self.json_response_edit) # Main layout main_layout = QVBoxLayout() main_layout.setSpacing(20) main_layout.addLayout(question_submit_layout) main_layout.addLayout(json_response_layout) main_layout.setContentsMargins(30, 30, 30, 30) self.setLayout(main_layout) self.show()如何让按钮文本内容竖着显示?
你可以使用QPushButton的setStyleSheet方法设置按钮的样式表,将文本旋转90度,例如:
```
self.submit_btn.setStyleSheet('color: #fff; background-color: #20639b; border: none; font-size: 24px; padding: 10px; border-radius: 5px; transform: rotate(90deg);')
```
这里使用了transform属性将文本旋转了90度。
阅读全文