Tkinter学习笔记(二)

2020-11-02 14:13:41 浏览数 (1)

继续之前的学习,没什么好说的。

7.Checkbutton学习笔记

代码语言:javascript复制
import tkinter as tk

window=tk.Tk()
window.geometry('500x300')
showText=tk.StringVar()
labelOne=tk.Label(window,textvariable=showText,width=40,bg='red')
labelOne.pack()
def show():
    v1=int(var.get())
    v2=int(var1.get())
    print(v1)
    print(v2)
    if v1==1 and v2==1:
        showText.set('select all')
    elif v1==0 and v2==1:
        showText.set('select two')
    elif v1==1 and v2==0:
        showText.set('select one')
    else:
        showText.set('no select ')

var=tk.StringVar()
var1=tk.StringVar()
#定义两个多选框
CheckOne=tk.Checkbutton(window,text='java',variable=var,onvalue=1,offvalue=0,command=show)
CheckTwo=tk.Checkbutton(window,text='python',variable=var1,onvalue=1,offvalue=0,command=show)
CheckOne.pack()
CheckTwo.pack()
window.mainloop()

8.Scale组件学习

代码语言:javascript复制
import tkinter as tk
window=tk.Tk()
window.geometry('500x300')
showText=tk.StringVar()
lableOne=tk.Label(window,textvariable=showText,width=50,bg='red')
lableOne.pack()
def show(value):
    print(value)
    showText.set(value)
scaleOne=tk.Scale(window,label='scale',from_=0,to=100,orient=tk.HORIZONTAL,length=600,showvalue=0,tickinterval=1,resolution=1,command=show)
scaleOne.pack()
window.mainloop()

9.Canvas学习

代码语言:javascript复制
import tkinter as tk
window=tk.Tk()
window.geometry('500x300')
canvasOne=tk.Canvas(window,bg='red',height=500,width=200)


x0, y0, x1, y1 = 100, 100, 150, 150
line = canvasOne.create_line(x0-50, y0-50, x1-50, y1-50)                   # 画直线
oval = canvasOne.create_oval(x0 120, y0 50, x1 120, y1 50, fill='yellow')  # 画圆 用黄色填充
arc = canvasOne.create_arc(x0, y0 50, x1, y1 50, start=0, extent=180)      # 画扇形 从0度打开收到180度结束
rect = canvasOne.create_rectangle(330, 30, 330 20, 30 20)                  # 画矩形正方形

canvasOne.pack()
window.mainloop()

10.Menu学习

代码语言:javascript复制
import tkinter as tk
window=tk.Tk()
window.geometry('500x300')

#主菜单
mainMenu=tk.Menu(window)

first=tk.Menu(mainMenu,tearoff=0)
#将主菜单设置为空,其子菜单为first
mainMenu.add_cascade(label='文件',menu=first)

def new():
    print()


first.add_command(label='one',command=new)
first.add_command(label='two',command=new)
first.add_command(label='three',command=new)
first.add_separator()
first.add_command(label='exit',command=window.quit)

two=tk.Menu(mainMenu,tearoff=1)
mainMenu.add_cascade(label='设置',menu=two)
two.add_command(label='one',command=new)
two.add_command(label='one1',command=new)
two.add_command(label='one2',command=new)

window.config(menu=mainMenu)
window.mainloop()

11.Frame学习

代码语言:javascript复制
import tkinter as tk
window=tk.Tk()
window.geometry('500x300')

frame1=tk.Frame(window)
lable1=tk.Label(frame1,text='in frame1',bg='red').pack()
frame1.pack()
window.mainloop()

12.messagebox学习

代码语言:javascript复制
import tkinter as tk
import tkinter.messagebox
window=tk.Tk()
window.geometry('500x300')

def show():
    tkinter.messagebox.showinfo(title='hello',message='你好')
    tkinter.messagebox.showerror(title='1',message='error')
    print("三种:" tkinter.messagebox.askquestion(title='2',message='asdfsadfsa'))
    print(tkinter.messagebox.askyesno(title='3',message='asfsafsadfaf'))
    print(tkinter.messagebox.askyesnocancel(title='4',message='asfasfsfsfsf'))

tk.Button(window,text='in frame1',command=show).pack()

window.mainloop()

至此,组件部分就学完了,对于布局其实很难。所以还是放弃采用grid的方式。我觉得直接设置位置挺好的。

代码语言:javascript复制
button1=tk.Button(window,text='in frame1',command=show)
button1.place(x=100,y=100)
#这里用place直接放,不要用pack了
window.mainloop()

而对于要显示一个子窗口的情况,可以使用newwindow = tk.Toplevel(window)来创建。

代码语言:javascript复制
    newwindow = tk.Toplevel(window)
    newwindow.geometry('400x67')

至此Tkinker学习完了,你知道以后去哪里找资料了吗?对,就是这里!

0 人点赞