我想知道如何在Windows中创建无默认边框的应用程序,特别是标题栏,包括最小化、最大化和关闭按钮。
我正在考虑编写一个滚动条程序,它占据屏幕顶部或底部的狭窄空间,但除非可以在Python中创建一个细长的应用程序,否则我不会尝试它。感谢术语方面的帮助;也许我不知道如何搜索正确的问题。Tkinter有这个选项吗?
谢谢
我想知道如何在Windows中创建无默认边框的应用程序,特别是标题栏,包括最小化、最大化和关闭按钮。
我正在考虑编写一个滚动条程序,它占据屏幕顶部或底部的狭窄空间,但除非可以在Python中创建一个细长的应用程序,否则我不会尝试它。感谢术语方面的帮助;也许我不知道如何搜索正确的问题。Tkinter有这个选项吗?
谢谢
我在这里找到了一个回答我的问题的例子。overrideredirect(1)
是关键函数。
我喜欢这种方法,因为我熟悉Tk并且更喜欢Tk解决方案,但是请参考其他答案获取备选解决方案。
import tkMessageBox
from Tkinter import *
class App():
def __init__(self):
self.root = Tk()
self.root.overrideredirect(1)
self.frame = Frame(self.root, width=320, height=200,
borderwidth=2, relief=RAISED)
self.frame.pack_propagate(False)
self.frame.pack()
self.bQuit = Button(self.frame, text="Quit",
command=self.root.quit)
self.bQuit.pack(pady=20)
self.bHello = Button(self.frame, text="Hello",
command=self.hello)
self.bHello.pack(pady=20)
def hello(self):
tkMessageBox.showinfo("Popup", "Hello!")
app = App()
app.root.mainloop()
只需要添加您自己的关闭按钮或退出方法。
QtCore.Qt.FramelessWindowHint
。下面的代码只是证明这是可能的,而并不试图非常有用。特别地,你将不得不强制杀死应用程序才能关闭它。在一个适当的实现中,你需要以自定义方式处理鼠标事件,以允许用户移动和关闭应用程序。要运行此代码,你需要安装PySide。import sys
from PySide import QtGui, QtCore
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow(parent=None, flags=QtCore.Qt.FramelessWindowHint)
MainFrame = QtGui.QFrame(MainWindow)
MainWindow.setCentralWidget(MainFrame)
MainFrameLayout = QtGui.QVBoxLayout(MainFrame)
label = QtGui.QLabel('A Label')
MainFrameLayout.addWidget(label)
MainWindow.show()
sys.exit(app.exec_())
from TestUI import Ui_MainWindow
class testQT4(QtGui.QMainWindow):
def __init__(self, parent=None):
super(testQT4, self).__init__(parent,Qt.CustomizeWindowHint)
self.ui = Ui_MainWindow()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = testQT4()
myapp.show()
sys.exit(app.exec_())
TestUI是您的UI文件,通过使用“cmd”进入项目目录(通过cd[空格][您的路径]),然后键入以下内容创建:
pyuic4 resfile.ui -o TestUI.py
以上操作将在项目文件夹中创建TestUI.py。
resfile.ui是您在QT Designer上创建的文件。
希望这能帮到您。