如何放置工具栏 - PyQt4
How to place toolbar - PyQt4
如何将工具栏放置在屏幕的 right/left 一侧。我尝试使用:
self.formatbar = self.addToolBar("Format")
self.formatbar.setAllowedAreas(Qt.RightToolBarArea)
我没有收到任何错误,但工具栏仍在顶部。
setAllowedAreas
定义工具栏在被用户拖动时 允许 的区域(默认是 Qt.AllToolBarAreas
所以你仍然可以想改变这个)。要实际将其放置在特定的一侧,您需要在添加它时指定它。例如:
self.formatbar = QToolBar()
self.addToolBar( Qt.LeftToolBarArea , self.formatbar )
我正在为同一问题寻求帮助,并遇到了两个关于该主题的 Stack Overflow 问题(包括这个问题)。
当我测试建议的代码时,我偶然发现了一个似乎合适的解决方案。
tool_bar.setStyleSheet("font-family: Arial; font-size: 12pt; border-top: none; margin-top: 10px;")
您可以检查我的代码以更好地理解我试图实现的目标。我在 MenuBar 和 ToolBar 代码行之间放置了 headers 以帮助找到您希望看到的行。
from PyQt5.QtWidgets import *
from PyQt5 import QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
font = QFont("Arial", 12)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("PyQt5 | Positioning the ToolBar")
self.setWindowIcon(QtGui.QIcon("IMAGES II/PyQt5_Icon.ico"))
self.setFont(font)
self.setStyleSheet("background-color: #000000")
self.resize(1250, 750)
self.gui()
def gui(self):
self.widgets()
self.center_window()
self.show()
def widgets(self):
# ==================== Creating the MenuBar ====================
menu_bar = self.menuBar()
menu_bar.setStyleSheet("font-size: 12pt; color: #716E6E")
# ==================== Create the MenuBar Items ====================
file_menu = menu_bar.addMenu("File")
edit_menu = menu_bar.addMenu("Edit")
view_menu = menu_bar.addMenu("View")
synchronize_menu = menu_bar.addMenu("Synchronize")
window_menu = menu_bar.addMenu("Window")
help_menu = menu_bar.addMenu("Help")
# ==================== Create the File Menu's Submenus and Menu Items ====================
# Create the Submenu(s):
file_submenu = QMenu("New", self)
file_submenu.setStyleSheet("font-size: 12pt; color: #716E6E")
file_properties_submenu = QMenu("File Properties", self)
file_properties_submenu.setStyleSheet("font-size: 12pt; color: #716E6E")
# This is a submenu within the file_properties_submenu
line_separators = QMenu("Line Separators", self)
line_separators.setStyleSheet("font-size: 12pt; color: #716E6E")
# Create file_submenu Items:
new_file = QAction("New File", self)
directory = QAction("Directory", self)
python_file = QAction("Python File", self)
html_file = QAction("HTML File", self)
open = QAction("Open", self)
save_as = QAction("Save as", self)
settings = QAction("Settings", self)
exit = QAction("Exit", self)
exit.triggered.connect(self.close_app)
# Add Items to file_submenu Submenu:
file_submenu.addAction(new_file)
file_submenu.addAction(directory)
file_submenu.addSeparator()
file_submenu.addAction(python_file)
file_submenu.addAction(html_file)
# Create file_properties_submenu Items:
file_encoding = QAction("File Encoding", self)
remove_bom = QAction("Remove BOM", self)
add_bom = QAction("Add BOM", self)
associate_with_file_type = QAction("Associate with File Type", self)
# Create line_separators_submenu Items:
crlf_windows = QAction("CRLF - Windows", self)
lf_unix_and_macos = QAction("LF - Unix and macos", self)
cs_classic_macos = QAction("CS - Classic macos", self)
# Add Items to line_separatos submenu:
line_separators.addAction(crlf_windows)
line_separators.addAction(lf_unix_and_macos)
line_separators.addAction(cs_classic_macos)
# Add Items and Submenus to file_properties_submenu:
file_properties_submenu.addAction(file_encoding)
file_properties_submenu.addAction(remove_bom)
file_properties_submenu.addAction(add_bom)
file_properties_submenu.addSeparator()
file_properties_submenu.addAction(associate_with_file_type)
file_properties_submenu.addMenu(line_separators)
# Add Submenus and Menu Items to File Menu:
file_menu.addMenu(file_submenu)
file_menu.addAction(open)
file_menu.addAction(save_as)
file_menu.addSeparator()
file_menu.addAction(settings)
file_menu.addMenu(file_properties_submenu)
file_menu.addSeparator()
file_menu.addAction(exit)
# ==================== Edit Menu Submenus and Menu Items ====================
# Create Menu Items:
undo_ = QAction("Undo", self)
redo_ = QAction("Redo", self)
cut_ = QAction("Cut", self)
copy_ = QAction("Copy", self)
paste_ = QAction("Paste", self)
# Add Menu Items to Edit Menu:
edit_menu.addAction(undo_)
edit_menu.addAction(redo_)
edit_menu.addSeparator()
edit_menu.addAction(cut_)
edit_menu.addAction(copy_)
edit_menu.addAction(paste_)
# ==================== Creating a Vertical ToolBar ====================
# Create the ToolBar
tool_bar = self.addToolBar("")
# Position the ToolBar on the left side of the Main Window
self.addToolBar(Qt.LeftToolBarArea, tool_bar)
# Prevent the User from moving the ToolBar
tool_bar.setMovable(False)
# Enable descriptive text to be displayed under the
# ToolButton Icons
tool_bar.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
# Set font family, font size, and color of text for ToolBar Buttons
tool_bar.setStyleSheet("font-family: Arial; font-size: 10pt; border-top: none; margin-top: 10px")
# Set the size of the ToolButton Icons
tool_bar.setIconSize(QSize(35, 35))
# Create ToolBar item-icons with text
python_tool_bar_btn = QAction(QtGui.QIcon("IMAGES I/python.png"), "Python", self)
python_tool_bar_btn.setToolTip("Python Programming")
c_tool_bar_btn = QAction(QtGui.QIcon("IMAGES I/c-file.png"), "C", self)
c_tool_bar_btn.setToolTip("C Programming")
java_tool_bar_btn = QAction(QtGui.QIcon("IMAGES I/java.png"), "Java", self)
java_tool_bar_btn.setToolTip("Java Programming")
javascript_tool_bar_btn = QAction(QtGui.QIcon("IMAGES I/javascript.png"), "Javascript", self)
javascript_tool_bar_btn.setToolTip("Javascript Programming")
swift_tool_bar_btn = QAction(QtGui.QIcon("IMAGES I/swift.png"), "Swift", self)
swift_tool_bar_btn.setToolTip("Swift Programming")
stack_overflow_tool_bar_btn = QAction(QtGui.QIcon("IMAGES I/stack-overflow.png"), "Stack\nOverflow", self)
stack_overflow_tool_bar_btn.setToolTip("Stack Overflow")
# Add item-icons to ToolBar
tool_bar.addAction(python_tool_bar_btn)
tool_bar.addAction(c_tool_bar_btn)
tool_bar.addAction(java_tool_bar_btn)
tool_bar.addAction(javascript_tool_bar_btn)
tool_bar.addAction(swift_tool_bar_btn)
tool_bar.addAction(stack_overflow_tool_bar_btn)
# Define a single function that provides functionality
# to all the ToolBar item-icons
tool_bar.actionTriggered.connect(self.tool_bar_actions)
def tool_bar_actions(self, tool_bar_btn):
if tool_bar_btn.text() == "Python":
print("Python and PyQt5 GUI Programming")
elif tool_bar_btn.text() == "C":
print("C Programming")
elif tool_bar_btn.text() == "Java":
print("Java Programming")
elif tool_bar_btn.text() == "Javascript":
print("Javascript Programming")
elif tool_bar_btn.text() == "Swift":
print("Swift Programming")
else:
print("Stack Overflow Developer's Community")
def close_app(self):
self.close()
def center_window(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
def main():
App = QApplication(sys.argv)
App.setStyleSheet(
"""
QMenuBar { background-color: #000000; font-family: Arial; font-size: 12pt; color: #0476B3; }
QMenuBar::item:selected { background-color: #c8e5ff; color: #0476B3; }
QMenu::item:selected { background-color: #c8e5ff; color: #0476B3; }
"""
)
main_window = MainWindow()
sys.exit(App.exec_())
if __name__ == '__main__':
main()
如何将工具栏放置在屏幕的 right/left 一侧。我尝试使用:
self.formatbar = self.addToolBar("Format")
self.formatbar.setAllowedAreas(Qt.RightToolBarArea)
我没有收到任何错误,但工具栏仍在顶部。
setAllowedAreas
定义工具栏在被用户拖动时 允许 的区域(默认是 Qt.AllToolBarAreas
所以你仍然可以想改变这个)。要实际将其放置在特定的一侧,您需要在添加它时指定它。例如:
self.formatbar = QToolBar()
self.addToolBar( Qt.LeftToolBarArea , self.formatbar )
我正在为同一问题寻求帮助,并遇到了两个关于该主题的 Stack Overflow 问题(包括这个问题)。
当我测试建议的代码时,我偶然发现了一个似乎合适的解决方案。
tool_bar.setStyleSheet("font-family: Arial; font-size: 12pt; border-top: none; margin-top: 10px;")
您可以检查我的代码以更好地理解我试图实现的目标。我在 MenuBar 和 ToolBar 代码行之间放置了 headers 以帮助找到您希望看到的行。
from PyQt5.QtWidgets import *
from PyQt5 import QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
font = QFont("Arial", 12)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("PyQt5 | Positioning the ToolBar")
self.setWindowIcon(QtGui.QIcon("IMAGES II/PyQt5_Icon.ico"))
self.setFont(font)
self.setStyleSheet("background-color: #000000")
self.resize(1250, 750)
self.gui()
def gui(self):
self.widgets()
self.center_window()
self.show()
def widgets(self):
# ==================== Creating the MenuBar ====================
menu_bar = self.menuBar()
menu_bar.setStyleSheet("font-size: 12pt; color: #716E6E")
# ==================== Create the MenuBar Items ====================
file_menu = menu_bar.addMenu("File")
edit_menu = menu_bar.addMenu("Edit")
view_menu = menu_bar.addMenu("View")
synchronize_menu = menu_bar.addMenu("Synchronize")
window_menu = menu_bar.addMenu("Window")
help_menu = menu_bar.addMenu("Help")
# ==================== Create the File Menu's Submenus and Menu Items ====================
# Create the Submenu(s):
file_submenu = QMenu("New", self)
file_submenu.setStyleSheet("font-size: 12pt; color: #716E6E")
file_properties_submenu = QMenu("File Properties", self)
file_properties_submenu.setStyleSheet("font-size: 12pt; color: #716E6E")
# This is a submenu within the file_properties_submenu
line_separators = QMenu("Line Separators", self)
line_separators.setStyleSheet("font-size: 12pt; color: #716E6E")
# Create file_submenu Items:
new_file = QAction("New File", self)
directory = QAction("Directory", self)
python_file = QAction("Python File", self)
html_file = QAction("HTML File", self)
open = QAction("Open", self)
save_as = QAction("Save as", self)
settings = QAction("Settings", self)
exit = QAction("Exit", self)
exit.triggered.connect(self.close_app)
# Add Items to file_submenu Submenu:
file_submenu.addAction(new_file)
file_submenu.addAction(directory)
file_submenu.addSeparator()
file_submenu.addAction(python_file)
file_submenu.addAction(html_file)
# Create file_properties_submenu Items:
file_encoding = QAction("File Encoding", self)
remove_bom = QAction("Remove BOM", self)
add_bom = QAction("Add BOM", self)
associate_with_file_type = QAction("Associate with File Type", self)
# Create line_separators_submenu Items:
crlf_windows = QAction("CRLF - Windows", self)
lf_unix_and_macos = QAction("LF - Unix and macos", self)
cs_classic_macos = QAction("CS - Classic macos", self)
# Add Items to line_separatos submenu:
line_separators.addAction(crlf_windows)
line_separators.addAction(lf_unix_and_macos)
line_separators.addAction(cs_classic_macos)
# Add Items and Submenus to file_properties_submenu:
file_properties_submenu.addAction(file_encoding)
file_properties_submenu.addAction(remove_bom)
file_properties_submenu.addAction(add_bom)
file_properties_submenu.addSeparator()
file_properties_submenu.addAction(associate_with_file_type)
file_properties_submenu.addMenu(line_separators)
# Add Submenus and Menu Items to File Menu:
file_menu.addMenu(file_submenu)
file_menu.addAction(open)
file_menu.addAction(save_as)
file_menu.addSeparator()
file_menu.addAction(settings)
file_menu.addMenu(file_properties_submenu)
file_menu.addSeparator()
file_menu.addAction(exit)
# ==================== Edit Menu Submenus and Menu Items ====================
# Create Menu Items:
undo_ = QAction("Undo", self)
redo_ = QAction("Redo", self)
cut_ = QAction("Cut", self)
copy_ = QAction("Copy", self)
paste_ = QAction("Paste", self)
# Add Menu Items to Edit Menu:
edit_menu.addAction(undo_)
edit_menu.addAction(redo_)
edit_menu.addSeparator()
edit_menu.addAction(cut_)
edit_menu.addAction(copy_)
edit_menu.addAction(paste_)
# ==================== Creating a Vertical ToolBar ====================
# Create the ToolBar
tool_bar = self.addToolBar("")
# Position the ToolBar on the left side of the Main Window
self.addToolBar(Qt.LeftToolBarArea, tool_bar)
# Prevent the User from moving the ToolBar
tool_bar.setMovable(False)
# Enable descriptive text to be displayed under the
# ToolButton Icons
tool_bar.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
# Set font family, font size, and color of text for ToolBar Buttons
tool_bar.setStyleSheet("font-family: Arial; font-size: 10pt; border-top: none; margin-top: 10px")
# Set the size of the ToolButton Icons
tool_bar.setIconSize(QSize(35, 35))
# Create ToolBar item-icons with text
python_tool_bar_btn = QAction(QtGui.QIcon("IMAGES I/python.png"), "Python", self)
python_tool_bar_btn.setToolTip("Python Programming")
c_tool_bar_btn = QAction(QtGui.QIcon("IMAGES I/c-file.png"), "C", self)
c_tool_bar_btn.setToolTip("C Programming")
java_tool_bar_btn = QAction(QtGui.QIcon("IMAGES I/java.png"), "Java", self)
java_tool_bar_btn.setToolTip("Java Programming")
javascript_tool_bar_btn = QAction(QtGui.QIcon("IMAGES I/javascript.png"), "Javascript", self)
javascript_tool_bar_btn.setToolTip("Javascript Programming")
swift_tool_bar_btn = QAction(QtGui.QIcon("IMAGES I/swift.png"), "Swift", self)
swift_tool_bar_btn.setToolTip("Swift Programming")
stack_overflow_tool_bar_btn = QAction(QtGui.QIcon("IMAGES I/stack-overflow.png"), "Stack\nOverflow", self)
stack_overflow_tool_bar_btn.setToolTip("Stack Overflow")
# Add item-icons to ToolBar
tool_bar.addAction(python_tool_bar_btn)
tool_bar.addAction(c_tool_bar_btn)
tool_bar.addAction(java_tool_bar_btn)
tool_bar.addAction(javascript_tool_bar_btn)
tool_bar.addAction(swift_tool_bar_btn)
tool_bar.addAction(stack_overflow_tool_bar_btn)
# Define a single function that provides functionality
# to all the ToolBar item-icons
tool_bar.actionTriggered.connect(self.tool_bar_actions)
def tool_bar_actions(self, tool_bar_btn):
if tool_bar_btn.text() == "Python":
print("Python and PyQt5 GUI Programming")
elif tool_bar_btn.text() == "C":
print("C Programming")
elif tool_bar_btn.text() == "Java":
print("Java Programming")
elif tool_bar_btn.text() == "Javascript":
print("Javascript Programming")
elif tool_bar_btn.text() == "Swift":
print("Swift Programming")
else:
print("Stack Overflow Developer's Community")
def close_app(self):
self.close()
def center_window(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
def main():
App = QApplication(sys.argv)
App.setStyleSheet(
"""
QMenuBar { background-color: #000000; font-family: Arial; font-size: 12pt; color: #0476B3; }
QMenuBar::item:selected { background-color: #c8e5ff; color: #0476B3; }
QMenu::item:selected { background-color: #c8e5ff; color: #0476B3; }
"""
)
main_window = MainWindow()
sys.exit(App.exec_())
if __name__ == '__main__':
main()