树莓派python界面编程_树莓派入门5 GUI编程1 图形界面

图形界面的构造

使用的框架为:

python-tk

python3-pyside

编辑新文件,命名为GUI.py,粘贴代码,使用下面的命令运行:

python GUI.py

代码片段如下所示:

# create demo

import turtle

import sys

from PySide.QtCore import *

from PySide.QtGui import *

class TurtleControl(QWidget):

def __init__(self, turtle):

super(TurtleControl, self).__init__()

self.turtle = turtle

self.left_btn = QPushButton("Left", self)

self.right_btn = QPushButton("Right", self)

self.move_btn = QPushButton("Move", self)

self.distance_spin = QSpinBox()

self.controlsLayout = QGridLayout()

self.controlsLayout.addWidget(self.left_btn, 0, 0)

self.controlsLayout.addWidget(self.right_btn, 0, 1)

self.controlsLayout.addWidget(self.distance_spin, 1, 0)

self.controlsLayout.addWidget(self.move_btn, 1, 1)

self.setLayout(self.controlsLayout)

self.distance_spin.setRange(0, 100)

self.distance_spin.setSingleStep(5)

self.distance_spin.setValue(20)

self.move_btn.clicked.connect(self.move_turtle)

self.right_btn.clicked.connect(self.turn_turtle_right)

self.left_btn.clicked.connect(self.turn_turtle_left)

# set up turtles

def turn_turtle_left(self):

self.turtle.left(45)

def turn_turtle_right(self):

self.turtle.right(45)

def move_turtle(self):

self.turtle.forward(self.distance_spin.value())

window = turtle.Screen()

babbage = turtle.Turtle()

# create a Qt application

app = QApplication(sys.argv)

control_window = TurtleControl(babbage)

control_window.show()

# enter Qt application main loop

app.exec_()

sys.exit()

运行即可,结束


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部