from tkinter import *
root = Tk()
w1 = Message(root, text='这是一则消息', width=100)
w1.pack()
w2 = Message(root, text='这是一则骇人听闻的长长长长长长长长长长消息', width=100)
w2.pack()
mainloop()
============
from tkinter import *
root = Tk()
w1 = Spinbox(root, from_=0, to=10)
w1.pack()
mainloop()
===============
from tkinter import *
root = Tk()
w = Spinbox(root, values=('小甲鱼', '风介', 'wei', '戴宇轩'))
w.pack()
mainloop()
===============
from tkinter import *
m = PanedWindow(orient=VERTICAL)
m.pack(fill=BOTH, expand=1)
top = Label(m, text='Top Pane')
m.add(top)
bottom = Label(m, text='Bottom Pane')
m.add(bottom)
mainloop()
==============
from tkinter import *
m = PanedWindow(orient=VERTICAL)#默认为左右,vertical为垂直分割
#BOTH表示全局填充,expand表示有1条分割线
m.pack(fill=BOTH, expand=1)
top = Label(m, text='Top Pane')
m.add(top)
bottom = Label(m, text='Bottom Pane')
m.add(bottom)
mainloop()
============
from tkinter import *
m1 = PanedWindow(showhandle=True, sashrelief=SUNKEN)
#是否显示handle小方块,分割线样式,往下凹
m1.pack(fill=BOTH, expand=1)
left = Label(m1, text='LEFT Pane')
m1.add(left)
m2 = PanedWindow(orient=VERTICAL, showhandle=True, sashrelief=SUNKEN)
m1.add(m2)
top = Label(m2, text='Top Pane')
m2.add(top)
bottom = Label(m2, text='Bottom Pane')
m2.add(bottom)
mainloop()
=============
from tkinter import *
root = Tk()
def create():
top = Toplevel()
top.title('FishC Demo')
top.attributes('-alpha', 0.5)
msg = Message(top, text='大家好,我是渣渣辉!')
msg.pack()
Button(root, text='创建顶级窗口', command=create).pack()
mainloop()
==============
from tkinter import *
root = Tk()
listbox = Listbox(root)
listbox.pack(fill=BOTH, expand=True)#True是指窗口拓展的时候也能填充进去
#fill=X表示横向填充,fill=Y表示纵向填充,fill=BOTH是横向和纵向
for i in range(10):
listbox.insert(END, str(i))
mainloop()
#pack默认是纵向填充
===============
from tkinter import *
root = Tk()
Label(root, text='用户名').grid(row=0, column=0, sticky=W)
Label(root, text='密码').grid(row=1, column=0, sticky=W)
photo = PhotoImage('logo.gif')
Label(root, image=photo).grid(row=0, column=2, rowspan=2, padx=5, pady=5)
Entry(root).grid(row=0, column=1)
Entry(root, show='*').grid(row=1, column=1)
Button(text='提交', width=10).grid(row=2, column=0, columnspan=3, pady=5)
mainloop()
=================
from tkinter import *
root = Tk()
photo = PhotoImage('logo_big.gif')
Label(root, image=photo).pack()
def callback():
print('正中靶心!')
Button(root, text='点我!', command=callback)\
.place(relx=0.5, rely=0.5, anchor=CENTER)
#相对距离 0.5表示正中心,
mainloop()
==============
from tkinter import *
root = Tk()
Label(root, bg='red').place(relx=0.5, rely=0.5, \
relheight=0.75, relwidth=0.75, anchor=CENTER)
#下个组件的相对高度和宽度
Label(root, bg='yellow').place(relx=0.5, rely=0.5, \
relheight=0.5, relwidth=0.5, anchor=CENTER)
Label(root, bg='green').place(relx=0.5, rely=0.5, \
relheight=0.25, relwidth=0.25, anchor=CENTER)
mainloop()
from tkinter import *
from tkinter import messagebox
print(messagebox.askokcancel('FishC demp', '发射核弹?'))
mainloop()
================
from tkinter import *
from tkinter.filedialog import askopenfilename
root = Tk()
def callback():
fileName = askopenfilename(filetypes=[('PNG', '.PNG'), ('JPG', '.jpg'), ('GIF', '.gif'), ('PYTHON', '.py')])
print(fileName)
Button(root, text='打开文件', command=callback).pack()
mainloop()
==================
from tkinter import *
from tkinter.colorchooser import askcolor
root = Tk()
def callback():
filename = askcolor()
print(filename)
Button(root, text='选择颜色', command=callback).pack()
mainloop()
================
import pygame
import sys
from pygame.locals import *
pygame.init()
clock = pygame.time.Clock()
size = width, height = 1200, 800
speed = [-2, 1]
bg = (255, 255, 255)
screen = pygame.display.set_mode(size)
pygame.display.set_caption('初次见面,请大家多多关照!')
turtle = pygame.image.load(r'E:\vippython\lena.jpg')
position = turtle.get_rect()
l_head = turtle
r_head = pygame.transform.flip(turtle, True, False)
while(True):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == KEYDOWN:
if event.key == K_LEFT:
turtle = l_head
speed = [-1, 0]
if event.key == K_RIGHT:
turtle = r_head
speed = [1, 0]
if event.key == K_UP:
speed = [0, -1]
if event.key == K_DOWN:
speed = [0, 1]
position = position.move(speed)
if position.left < 0 or position.right > width:
pygame.transform.flip(turtle, True, False)
speed[0] = -speed[0]
if position.top < 0 or position.bottom > height:
speed[1] = -speed[1]
screen.fill(bg)
screen.blit(turtle, position)
pygame.display.flip()
clock.tick(200)#最高的帧率
pygame.time.delay(10)
#surface对象是pygame里面用来表示图像的对象
================81.1
import pygame
import sys
pygame.init()
size = width, height = 600, 400
screen = pygame.display.set_mode(size)
pygame.display.set_caption('初次见面,请多关照!!')
f = open('record.txt', 'w')
while True:
for event in pygame.event.get():
f.write(str(event) + '\n')
if event.type == pygame.QUIT:
f.close()
sys.exit()
#(0, 0, 0)左键 中键 右键 是否被按下 pos图像中的位置, 相对于上一个的位置
#key是ascall码
==================81.2
import sys
import pygame
pygame.init()
size = width, height = 600, 400
bg = (0, 0, 0)
screen = pygame.display.set_mode(size)
pygame.display.set_caption('FishC Demo')
font = pygame.font.Font(None, 20)
line_height = font.get_linesize()
position = 0
screen.fill(bg)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.blit(font.render(str(event), True, (0, 255, 0)), (0, position))
position += line_height
if position > height:
position = 0
screen.fill(bg)
pygame.display.flip()
===================82.1
import pygame
import sys
from pygame.locals import *
pygame.init()
clock = pygame.time.Clock()
size = width, height = 1200, 800
bg = (255, 255, 255)
fullscreen = False
ratio = 1.0
screen = pygame.display.set_mode(size, RESIZABLE)
pygame.display.set_caption('初次见面,请大家多多关照!')
oturtle = pygame.image.load(r'E:\vippython\lena.jpg')
turtle = oturtle
oturtle_rect = oturtle.get_rect()
position = turtle_rect = oturtle_rect
speed = [5, 0]
turtle_right = pygame.transform.rotate(turtle, 90)
turtle_top = pygame.transform.rotate(turtle, 180)
turtle_left = pygame.transform.rotate(turtle, 270)
turtle_bottom = turtle
turtle = turtle_top
l_head = turtle
r_head = pygame.transform.flip(turtle, True, False)
while(True):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == KEYDOWN:
if event.key == K_LEFT:
turtle = l_head
speed = [-1, 0]
if event.key == K_RIGHT:
turtle = r_head
speed = [1, 0]
if event.key == K_UP:
speed = [0, -1]
if event.key == K_DOWN:
speed = [0, 1]
if event.key == K_F11:
fullscreen = not fullscreen
if fullscreen:
screen = pygame.display.set_mode((1920, 1080), FULLSCREEN | HWSURFACE)
width, height = 1920, 1080
else:
screen = pygame.display.set_mode(size)
if event.key == K_EQUALS or event.key == K_MINUS or event.key == K_SPACE:
if event.key == K_EQUALS and ratio < 2:
ratio += 0.1
if event.key == K_MINUS and ratio > 0.5:
ratio -= 0.1
if event.key == K_SPACE:
ratio = 1
turtle = pygame.transform.smoothscale(oturtle, (int(oturtle_rect.width * ratio), int(oturtle_rect.height * ratio)))
if event.type == VIDEORESIZE:
size = event.size
width, height = size
print(size)
screen = pygame.display.set_mode(size, RESIZABLE)
position = position.move(speed)
if position.right > width:
turtle = turtle_right
position = turtle_rect = turtle.get_rect()
position.left = width - turtle_rect.width
speed = [0, 5]
if position.bottom > height:
turtle = turtle_bottom
position = turtle_rect = turtle.get_rect()
position.left = width - turtle_rect.width
position.top = height - turtle_rect.height
speed = [-5, 0]
if position.left < 0:
turtle = turtle_left
position = turtle_rect = turtle.get_rect()
position.top = height - turtle_rect.height
speed = [0, -5]
if position.top < 0:
turtle = turtle_top
position = turtle_rect = turtle.get_rect()
speed = [5, 0]
if position.left < 0 or position.right > width:
pygame.transform.flip(turtle, True, False)
speed[0] = -speed[0]
if position.top < 0 or position.bottom > height:
speed[1] = -speed[1]
screen.fill(bg)
screen.blit(turtle, position)
pygame.display.flip()
clock.tick(200)#最高的帧率
pygame.time.delay(10)
====================82.2
#set_mode resolution指定界面尺寸 指定拓展选项 指定颜色位数
import pygame
pygame.init()
print(pygame.display.list_modes())
==================82.3
import pygame
import sys
pygame.init()
select = 0
drag = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == MOUSEBUTTONDOWN:
if event.button == 1:
if select == 0 and drag == 0:
select= 1
elif select == 2 and drag == 0:
drag = 1
elif select == 2 and drag == 2:
select = 2
elif event.type == MOUSEBUTTONUP:
if event.button == 1:
if select == 1 and drag == 0:
select = 2
if select == 2 and drag == 1:
drag = 2
screen.fill(bg)
screen.blit(turtle, position)
====================83.1
import pygame
import sys
from pygame.locals import *
pygame.init()
clock = pygame.time.Clock()
size = width, height = 640, 400
bg = (0, 0, 0)
screen = pygame.display.set_mode(size, RESIZABLE)
pygame.display.set_caption('FishC Demo')
turtle = pygame.image.load(r'E:\vippython\lena.jpg').convert()
background = pygame.image.load(r'E:\vippython\计数.png').convert()
position = turtle.get_rect()
position.center = width // 2, height // 2
while(True):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.blit(background, (0, 0))
screen.blit(turtle, position)
pygame.display.flip()
clock.tick(30)#最高的帧率
|