python GUI

@[TOP]

GUI Programming

先下载PySide库

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)
        
#set up turtle
window = turtle.Screen()
bage = turtle.Turtle()
        
#Create a Qt application
app = QApplication(sys.argv)
control_window = TurtleControl(bage)
control_window.show()
        
#Enter Qt application main loop
app.exec_()
sys.exit()     

Adding Controls

All above code doesn’t actually do anything. Then adding controls is to tell Qt what you want the controls to do.
To set this up, add the following code to the end of the init method of the TurtleControl class:

    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)
        
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())

完整代码

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)
        
    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())
        
#set up turtle
window = turtle.Screen()
bage = turtle.Turtle()
        
#Create a Qt application
app = QApplication(sys.argv)
control_window = TurtleControl(bage)
control_window.show()
        
#Enter Qt application main loop
app.exec_()
sys.exit()       

显示结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值