Python—作业11—GUI环境的基础应用

GUI环境是一种交互非常良好的方式,使得非编程人员进行相关操作,像是我们用的各种系统,也是如此,本文将以下题目放在下面,供大家学习参考:
 

  • 使用tkinter库完成下列程序

1、编程实现如下GUI(只显示,没有回调函数)

这道题是简单的控件创建:

import tkinter

root =tkinter.Tk()

root.geometry("200x50")
label =tkinter.Label(root,text='Welcome to Python Tkinter')
label.pack()

buttton= tkinter.Button(root,text='Click Me')
buttton.pack()

root.mainloop()

编程响应用户事件。“OK”按钮前景为红色,按下“OK”按钮显示"OK button is clicked",”Cancel“按钮背景为黄色,按下”Cancel“按钮显示"Cancel button is clicked"

 这道题是对控件加了一个回调函数:

import tkinter
def ok():
    print('OK button is clicked')

def Cancel():
    print('Cancel button is clicked')

root =tkinter.Tk()

root.geometry("200x50")

buttton1= tkinter.Button(root,text='OK',fg='red',command=ok)
buttton1.pack()

buttton2= tkinter.Button(root,text='Cancel',bg='yellow',command=Cancel)
buttton2.pack()
root.mainloop()
  • 使用Turtle库完成下列程序

1、绘制一个红色的五角星图形,如图所示。

这道题,网上有很多的代码,非常容易实现:

import math

import turtle

RADIUS = 100

angleSin18 = math.sin(math.pi * 0.1) * RADIUS

angleCos18 = math.cos(math.pi * 0.1) * RADIUS

angleSin54 = math.sin(math.pi * 0.3) * RADIUS

angleCos54 = math.cos(math.pi * 0.3) * RADIUS

turtle.width(1)

turtle.fillcolor("red")

turtle.begin_fill()

turtle.penup()

turtle.goto(-angleCos18, angleSin18)

turtle.pendown()

turtle.goto(angleCos18, angleSin18)

turtle.goto(-angleCos54, -angleSin54)

turtle.goto(0, RADIUS)

turtle.goto(angleCos54, -angleSin54)

turtle.goto(-angleCos18, angleSin18)

turtle.end_fill()

turtle.mainloop()

注意的是想让运行的结果长期显示,最后要加上turtle.mainloop()的命令。

螺旋线绘制(每一圈是四个线段组成,一共绘制100个线段)。绘制一个螺旋线的图形,如图所示。

这道题也是非常简单的一个命令操作:

import turtle as t
t.pen(speed=0)
t.penup()
t.goto(-100, 100)
t.pendown()
t.seth(0)
length = 400
while (length!=0):
    t.fd(length)
    t.right(90)
    length -= 4
t.done()

 


版权声明:本文为qq_45290891原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。