tkinter——python标准GUI库

 

# -*- coding:utf-8 -*-
 
import tkinter as tk
import random

window=tk.Tk()#一个窗口对象
window.title("tkinter")#窗口名称
window.geometry("200x200")#字符串形式表示长宽 中间是x



e=tk.Entry(window,show="{}".format(random.randint(0,9)))#输入框 输入显示 none为输入即所得
e.insert(0,"")#默认显示在输入框的内容
e.pack()

la=tk.Label(window,text="我是鬼畜窗口\nnb",bg="red",font=("Ariel",20),width=20,height=2,justify="left")
#所属窗口 bg显示字符背景颜色,font字体与字大小,长宽(以字大小为单位)justify行对其方式默认center
la.pack()#打包

var=tk.StringVar()#tk的字符变量对象

lb=tk.Label(window,textvariable=var,bg="blue",fg="red",font=("Ariel",20),width=20,height=1
    ,anchor="nw").pack()
#textvariable显示字符使用变量 fg字体颜色 anchor文字或者图片所在位置nw就是左上角默认居中


Hit_bu=1
def hit_b():
    global Hit_bu
    if Hit_bu==1:
        Hit_bu=2
        var.set("")#变量对象的设置
    elif(Hit_bu==2):
        Hit_bu=3
        var.set("")
    else:
        Hit_bu=1
        var.set("")

    text=e.get()#从输入框获取字符
    t.insert("insert",text)#在文本框插入 出入可以为inser在游标处插入 end在结尾插入 
                           #也可以用x.x表示在已有位置插入

b=tk.Button(window,text="hitme",command=hit_b,width=10,height=1)
b.pack()

t=tk.Text(window,height=2,width=10)#文本框
t.pack()

window.mainloop()#使窗口运行

 

#单选框
import tkinter as tk

win=tk.Tk()

l=tk.Label(win,text="what",width=50,height=10,bg="blue")
l.pack()
var=tk.StringVar()#字符串变量
r1=tk.Radiobutton(win,text="fuck",variable=var,value="A").pack()
r2=tk.Radiobutton(win,text="shit",variable=var,value="B").pack()
#这里将value传递给variable 使用tk.StringVar.get()返回值
def change():
    if(var.get()=="A"):
        l.config(bg="red",text="你GUI做的像菜虚鲲")#重新更改属性
    else:
        l.config(bg="green",text="你好骚啊")

b=tk.Button(win,text="asshole",width=8,height=1,command=change).pack()

win.mainloop()

 

 

 

 

 

图像处理简单GUI 可以平移旋转裁剪放缩

(做的还不如蔡徐坤 丑爆了

# -*- coding:utf-8 -*-
import tkinter as tk
from tkinter import filedialog
from PIL import Image,ImageTk
import matplotlib.pyplot as plt
#from Geograic import *

window=tk.Tk()
window.title("图像处理课程GUI")
window.geometry("1280x720")


img_tk=""
img=""


def openimage():
    global img_tk,img
    filepath = filedialog.askopenfilename()  
    img =Image.open(filepath)
    img_tk=ImageTk.PhotoImage(img)
    img_show= canvas.create_image(500,300,anchor='center', image=img_tk)

def saveimage():
    global img
    filesavepath = filedialog.asksaveasfilename()
    img.save(str(filesavepath) + '.png', 'PNG')

fr=tk.Frame(window)
canvas=tk.Canvas(fr, bg='blue',width=1000,height=600)
canvas.pack()

bopen=tk.Button(fr,command=openimage,text="Select image",width=10,height=2,bg="yellow")
bopen.pack()
bsave=tk.Button(fr,command=saveimage,text="Save image",width=10,height=2,bg="red")
bsave.pack()

fr.pack(side="right")

#################################画布\操作函数########################################

def do_job():
    print("")


def rotate():
    global img_tk,img
    img=img.rotate(int(e_Rotating.get()))
    #plt.imshow(img)
    #plt.show(img)
    img_tk=ImageTk.PhotoImage(img)
    img_show= canvas.create_image(500,300,anchor='center', image=img_tk)

def resize():
    global img_tk,img
    k=(int(e_Resizing_x.get()),int(e_Resizing_y.get()))
    img=img.resize(k)
    img_tk=ImageTk.PhotoImage(img)
    img_show= canvas.create_image(500,300,anchor='center', image=img_tk)

def cut():
    global img_tk,img
    box=(int(e_Cutting_x.get()),int(e_Cutting_y.get()),int(e_Cutting_x1.get()),int(e_Cutting_y1.get()))
    img=img.crop(box)
    img_tk=ImageTk.PhotoImage(img)
    img_show= canvas.create_image(500,300,anchor='center', image=img_tk)

def showauthor():
    tk.messagebox.showinfo(message="作者:张浩然\n学号:20171308074",title="Author Info")
#####################操作面板########################
fl=tk.Frame(window)

f_Translation=tk.Frame(fl)
l_Translation=tk.Label(f_Translation,text="translation\n输入x与y",width=10,height=2).pack()
e_movx=tk.Entry(f_Translation,show=None,width=5)
e_movx.pack()
e_movy=tk.Entry(f_Translation,show=None,width=5)
e_movy.pack()
b_Translation=tk.Button(f_Translation,text="Translation",command=do_job,width=10,height=1).pack()
f_Translation.pack()

f_Rotating=tk.Frame(fl)
l_Rotating=tk.Label(f_Rotating,text="Rotating\n输入旋转角度",width=10,height=2).pack()
e_Rotating=tk.Entry(f_Rotating,show=None,width=5)
e_Rotating.pack()
b_Rotating=tk.Button(f_Rotating,text="Rotating",command=rotate,width=10,height=1).pack()
f_Rotating.pack()

f_Resizing=tk.Frame(fl)
l_Resizing=tk.Label(f_Resizing,text="Resizing\n输入缩小后长宽",width=15,height=2).pack()
e_Resizing_x=tk.Entry(f_Resizing,show=None,width=5)
e_Resizing_x.pack()
e_Resizing_y=tk.Entry(f_Resizing,show=None,width=5)
e_Resizing_y.pack()
b_Resizing=tk.Button(f_Resizing,text="Resizing",command=resize,width=10,height=1).pack()
f_Resizing.pack()

f_Cutting=tk.Frame(fl)
l_Cutting=tk.Label(f_Cutting,text="Cutting\n输入裁剪的坐标(左上右下)",width=15,height=2).pack()
e_Cutting_x=tk.Entry(f_Cutting,show=None,width=5)
e_Cutting_x.pack()
e_Cutting_y=tk.Entry(f_Cutting,show=None,width=5)
e_Cutting_y.pack()
e_Cutting_x1=tk.Entry(f_Cutting,show=None,width=5)
e_Cutting_x1.pack()
e_Cutting_y1=tk.Entry(f_Cutting,show=None,width=5)
e_Cutting_y1.pack()#需要查询
b_Cutting=tk.Button(f_Cutting,text="Cutting",command=cut,width=10,height=1).pack()
f_Cutting.pack()

fl.pack(side="left")

#######################操作面版/菜单##################


menubar=tk.Menu(window)

filemenu=tk.Menu(menubar,tearoff=0)
menubar.add_cascade(label="File",menu=filemenu)
filemenu.add_command(label="Open",command=openimage)
filemenu.add_command(label="Save",command=saveimage)

setmenu=tk.Menu(menubar,tearoff=0)
menubar.add_cascade(label="Set",menu=setmenu)
setmenu.add_command(label="Exit",command=window.quit)
setmenu.add_command(label="Showauthor",command=showauthor)

window.config(menu=menubar)
#########################菜单##########################
window.mainloop()

 

posted on 2019-04-16 17:07  batt1ebear  阅读(601)  评论(0)    收藏  举报

导航