有没有办法检测用户是否试图关闭窗口?例如,在Tkinter中我们可以这样做:
def exit_dialog():
#do stuff
pass
root = Tk()
root.protocol("WM_DELETE_WINDOW", exit_dialog)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
谢谢.
我有一个点列表.例如,points = [[160, 75], [115, 567]]
.
如何在PyQt4中绘制一条线,所以它会是这样的:
提前致谢.
编辑:为了记录,我正在尝试实现Bezier曲线,所以它看起来像这样:
这是我目前的代码:
from PyQt4.QtGui import QWidget, QPolygonF, QPainter, QPen, QBrush, QColor, \
QApplication, QIcon, QVBoxLayout, QSlider, QHBoxLayout, QPushButton, QLCDNumber
from PyQt4.QtCore import QObject, SIGNAL, SLOT, QPointF, Qt, QRectF, QPointF
import time, sys
#================================================================
# For the next block I used this post
# http://stackoverflow.com/a/3220819/736306,
# but I wish to replace it with self.liniar_bez(),
# biliniar_bez(), self.cubic_bez() and self.fourG_bez()
# because I want to control 't' with self.slider
def avg(a, …
Run Code Online (Sandbox Code Playgroud) 我正在尝试为折线设置动画(它必须像波浪一样).我试过这种方式:
from PySide.QtCore import *
from PySide.QtGui import *
import sys, time
class Test(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
def poly(self, pts):
return QPolygonF(map(lambda p: QPointF(*p), pts))
def paintEvent(self, event):
painter = QPainter(self)
pts = [[80, 490], [180, 0], [280, 0], [430, 0], [580, 0], [680, 0], [780, 0]]
for i in pts:
while i[1] < 600:
painter.setPen(QPen(QColor(Qt.darkGreen), 3))
painter.drawPolyline(self.poly(pts))
painter.setBrush(QBrush(QColor(255, 0, 0)))
painter.setPen(QPen(QColor(Qt.black), 1))
for x, y in pts:
painter.drawEllipse(QRectF(x - 4, y - 4, 8, 8))
i[1] += …
Run Code Online (Sandbox Code Playgroud) 我正在尝试用Tkinter写简单的记事本.我需要一些字体选择器.所以我的问题是:是否包括一个?如果没有,我在哪里可以得到一个?
谢谢.
我试图在Python和Tkinter的帮助下编写一个简单的Arkanoid.目标是让球从顶部,右侧和左侧反射.如果球员错过球而接触到底部,则比赛将停止.
这是代码:
from Tkinter import *
import time
root = Tk()
canv = Canvas(root, highlightthickness=0)
canv.pack(fill='both', expand=True)
top = canv.create_line(0, 0, 640, 0, fill='green', tags=('top'))
left = canv.create_line(0, 0, 0, 480, fill='green', tags=('left'))
right = canv.create_line(639, 0, 639, 480, fill='green', tags=('right'))
bottom = canv.create_line(0, 478, 640, 478, fill='red', tags=('bottom'))
rect = canv.create_rectangle(270, 468, 365, 478, outline='black', fill='gray40', tags=('rect'))
ball = canv.create_oval(0, 20, 20, 40, outline='black', fill='gray40', tags=('ball'))
delta_x = delta_y = 3
new_x, new_y = delta_x, -delta_y
while True:
time.sleep(0.025) …
Run Code Online (Sandbox Code Playgroud) 这是我的问题细节:我有这些小部件 - QMenuBar,QTableWidget和QToolbar.这是我的代码示例:
import sys
from PySide import QtGui
class Example(QtGui.QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.header_lbls = ['Name', 'Second Name', 'Surname', 'Birth Date', 'Phone Number', 'Skype', 'E-mail']
self.table = QtGui.QTableWidget(10, 7)
self.table.setHorizontalHeaderLabels(self.header_lbls)
self.setCentralWidget(self.table)
#ACTIONS
self.createActions()
#MENUBAR
self.createMenus()
#TOOLBAR
self.createToolbar()
#STATUSBAR
self.creatStatusbar()
def contextMenuEvent(self, event):
self.menu = QtGui.QMenu(self.table)
self.menu.addAction(self.aboutAct)
self.menu.exec_(QtGui.QCursor.pos())
def createActions(self):
self.exitAct = QtGui.QAction('E&xit', self, shortcut='Ctrl+Q',
statusTip='Exit the application', triggered=app.exit)
def createMenus(self):
self.menubar = self.menuBar()
self.fileMenu = self.menuBar().addMenu("&File")
self.fileMenu.addAction(self.exitAct)
def createToolbar(self):
self.toolbar = self.addToolBar('Toolbar')
self.toolbar.addAction(self.settingsAct)
self.toolbar.addSeparator()
self.toolbar.addAction(self.exitAct)
def creatStatusbar(self):
self.statusBar() …
Run Code Online (Sandbox Code Playgroud) 我正在尝试从sqlite数据库输出数据.我在PyQt的例子中做过,但它不起作用.似乎数据库已打开,但代码提供错误,但事实并非如此.你能告诉我为什么以及如何解决这个问题?
#####################################################################
# test.py #
# ! /usr/bin/env python #
# -*- coding: utf-8 -*- #
#####################################################################
from PyQt4 import QtGui, QtCore, QtSql
from src.database import DBase
class Test(QtGui.QMainWindow):
def __init__(self):
super(Test, self).__init__()
self.tview = QtGui.QTableView()
self.setCentralWidget(self.tview)
model = QtSql.QSqlTableModel(self)
model.setTable("person")
model.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)
model.select()
model.setHeaderData(0, QtCore.Qt.Horizontal, QtCore.QObject.trUtf8(model, "?"))
model.setHeaderData(1, QtCore.Qt.Horizontal, QtCore.QObject.trUtf8(model, "Name"))
model.setHeaderData(2, QtCore.Qt.Horizontal, QtCore.QObject.trUtf8(model, "Lastname"))
self.tview.setModel(model)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
db = DBase()
db.connection_db()
window = Test()
window.resize(800, 600)
window.show()
sys.exit(app.exec_())
#####################################################################
# database.py # …
Run Code Online (Sandbox Code Playgroud)