所以我有这段代码:
from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton, QWidget, QAction, QTabWidget, QVBoxLayout, QHBoxLayout, QComboBox, QLabel
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
from lang import getLang
import sys, os, configparser
config = configparser.ConfigParser()
config.read("Settings.ini")
# Config get setting and change setting
#print(config.get("Main", "language"))
#config.set("Main", "language", "danish")
#with open("Settings.ini", "w") as cfg_file:
#config.write(cfg_file)
class App(QMainWindow):
def __init__(self):
super().__init__()
# Window Settings
self.x, self.y, self.w, self.h = 0, 0, 300, 200
self.setGeometry(self.x, self.y, self.w, self.h)
self.window = MainWindow(self)
self.setCentralWidget(self.window)
self.setWindowTitle("Window title") # Window Title
self.show()
class MainWindow(QWidget):
def __init__(self, parent):
super(QWidget, self).__init__(parent)
layout = QVBoxLayout(self)
# Run this after settings
self.lang = getLang(config.get("Main", "language"))
# Initialize tabs
tab_holder = QTabWidget() # Create tab holder
tab_1 = QWidget() # Tab one
tab_2 = QWidget() # Tab two
# Add tabs
tab_holder.addTab(tab_1, self.lang["tab_1_title"]) # Add "tab1" to the tabs holder "tabs"
tab_holder.addTab(tab_2, self.lang["tab_2_title"]) # Add "tab2" to the tabs holder "tabs"
# Create first tab
tab_1.layout = QVBoxLayout(self)
tab_2.layout = QVBoxLayout(self)
# Buttons
button_start = QPushButton(self.lang["btn_start"])
button_stop = QPushButton(self.lang["btn_stop"])
button_test = QPushButton(self.lang["btn_test"])
# Button Extra
button_start.setToolTip("This is a tooltip for the button!") # Message to show when mouse hover
button_start.clicked.connect(self.on_click)
button_stop.clicked.connect(self.on_click)
button_test.clicked.connect(self.on_click)
#button_start.setEnabled(False)
# comboBox
label_language = QLabel("Language")
combo_language = QComboBox(self)
combo_language.addItem(self.lang["language_danish"])
combo_language.addItem(self.lang["language_english"])
# Move widgets
combo_language.move(50, 150)
label_language.move(50, 50)
# Tab Binding
self.AddToTab(tab_1, button_start)
self.AddToTab(tab_1, button_stop)
self.AddToTab(tab_2, label_language)
self.AddToTab(tab_2, combo_language)
# Add tabs to widget
tab_1.setLayout(tab_1.layout)
tab_2.setLayout(tab_2.layout)
layout.addWidget(tab_holder)
self.setLayout(layout)
@pyqtSlot()
def on_click(self):
button = self.sender().text()
if button == self.lang["btn_start"]:
print("Dank")
elif button == self.lang["btn_stop"]:
print("Not dank")
def AddToTab(self, tab, obj):
tab.layout.addWidget(obj)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
当前,它在一个选项卡上创建一个包含2个按钮的窗口.一个“开始”和“停止”按钮,除了打印文本外,没有其他实际功能.
在选项卡2上,我有一个标签为“ Language:”(语言:)和一个包含“丹麦语”和“英语”的下拉菜单.
我不确定如何更改位置,因为我不能仅在文本标签,按钮和下拉菜单上使用.move,因为它们位于选项卡中.
例如,在第二个标签上,我希望标签“ Language:”位于下拉菜单的左侧.
解决方法:
引起此问题的原因是您使用QVBoxLayout设置每个选项卡内的小部件.布局的任务是管理小部件的位置和大小,因此除了没有必要之外,您不能使用move().
解决方案是为每个选项卡创建一个自定义布局,但是保持顺序将为每个选项卡创建一个自定义小部件,在第一个选项卡的情况下,我将继续使用QVBoxlayout,但要添加拉伸以使小部件保持在最高位置.在第二种情况下,我将结合使用QVBoxLayout和QHBoxLayout,QHBoxLayout将使用它来放置QLabel和QComboBox,而QVBoxLayout将使用它来将小部件推到顶部.
实现以上内容,出于明显的原因,我省略了lang,我们得到了以下内容:
from PyQt5 import QtCore, QtGui, QtWidgets
import sys, os, configparser
config = configparser.ConfigParser()
config.read("Settings.ini")
# Config get setting and change setting
#print(config.get("Main", "language"))
#config.set("Main", "language", "danish")
#with open("Settings.ini", "w") as cfg_file:
#config.write(cfg_file)
class App(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
# Window Settings
self.x, self.y, self.w, self.h = 0, 0, 300, 200
self.setGeometry(self.x, self.y, self.w, self.h)
self.window = MainWindow(self)
self.setCentralWidget(self.window)
self.setWindowTitle("Window title") # Window Title
self.show()
class GeneralWidget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(GeneralWidget, self).__init__(parent)
lay = QtWidgets.QVBoxLayout(self)
# Buttons
button_start = QtWidgets.QPushButton("start") #self.lang["btn_start"])
button_stop = QtWidgets.QPushButton("stop") #self.lang["btn_stop"])
# Button Extra
button_start.setToolTip("This is a tooltip for the button!") # Message to show when mouse hover
button_start.clicked.connect(self.on_click)
button_stop.clicked.connect(self.on_click)
lay.addWidget(button_start)
lay.addWidget(button_stop)
lay.addStretch()
@QtCore.pyqtSlot()
def on_click(self):
button = self.sender().text()
if button == self.lang["btn_start"]:
print("Dank")
elif button == self.lang["btn_stop"]:
print("Not dank")
class OptionsWidget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(OptionsWidget, self).__init__(parent)
lay = QtWidgets.QVBoxLayout(self)
hlay = QtWidgets.QHBoxLayout()
lay.addLayout(hlay)
lay.addStretch()
label_language = QtWidgets.QLabel("Language")
combo_language = QtWidgets.QComboBox(self)
combo_language.addItem("item1") #self.lang["language_danish"])
combo_language.addItem("item2") #self.lang["language_english"])
hlay.addWidget(label_language)
hlay.addWidget(combo_language)
class MainWindow(QtWidgets.QWidget):
def __init__(self, parent):
super(MainWindow, self).__init__(parent)
layout = QtWidgets.QVBoxLayout(self)
# Run this after settings
# self.lang = getLang(config.get("Main", "language"))
# Initialize tabs
tab_holder = QtWidgets.QTabWidget() # Create tab holder
tab_1 = GeneralWidget() # Tab one
tab_2 = OptionsWidget() # Tab two
# Add tabs
tab_holder.addTab(tab_1, "General") #self.lang["tab_1_title"]) # Add "tab1" to the tabs holder "tabs"
tab_holder.addTab(tab_2, "Options") #self.lang["tab_2_title"]) # Add "tab2" to the tabs holder "tabs"
layout.addWidget(tab_holder)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())