Python tkinter 所以控件的简单使用

from tkinter import *
import tkinter.messagebox
from tkinter import filedialog
from tkinter import colorchooser
import PIL.Image

def buttonClik():
    content = contentVar.get()
    print(content)
    contentVar.set("bye-bye")

def checkButtonClik():
    if checkValue.get() == 1:
        checklabel.config(text = "选择")
    elif checkValue.get() == 0:
        checklabel.config(text = "未选择")

def listCheck():
    var = listbox.get(listbox.curselection())
    box = tkinter.messagebox.askquestion(title="信息", message="确定选择"+str(var))
    if box=="yes":
        listboxValue.set(var)
    elif box=="no":
        listboxValue.set("")


def radio_function():
    if radioValue.get() == 1:
        radioLabel.config(text="选择了 A")
        tkinter.messagebox.showinfo(title="提示", message="选择了A")
    elif radioValue.get() == 2:
        radioLabel.config(text="选择了 B")
        tkinter.messagebox.showinfo(title="提示", message="选择了B")
    elif radioValue.get() == 3:
        radioLabel.config(text="选择了 C")
        tkinter.messagebox.showinfo(title="提示", message="选择了C")

def print_scale(v):
    labelScale.config(text="当前年份为 20"+v.zfill(2))

def do_job():
    labelScale.config(text="选择了目录")

def getFileName():
    global fileName
    fileName = tkinter.filedialog.askopenfilename()
    labelImage = tkinter.Label(root, bg="gray")
    labelImage.place(x=0, y=50, width=300, height=300)
    img = tkinter.PhotoImage(file=fileName)
    labelImage.config(image=img)
    labelImage.image = img

def getColor():
    filename = tkinter.colorchooser.askcolor()
    print(filename)

if __name__ == "__main__":
    root = tkinter.Tk()
    # root.resizable(False, False)
    root.geometry("640x480")
    root.title("this is 界面!")

    menubar = tkinter.Menu(root)
    fileMenu = tkinter.Menu(menubar, tearoff=False)
    menubar.add_cascade(label="File", menu=fileMenu)
    fileMenu.add_command(label="New", command=do_job)
    fileMenu.add_command(label="Open", command=do_job)
    fileMenu.add_command(label="Sava", command=do_job)
    fileMenu.add_separator()
    fileMenu.add_command(label="Quit", command=root.quit)
    fileMenu1 = tkinter.Menu(menubar, tearoff=False)
    menubar.add_cascade(label="Edit", menu=fileMenu1)
    root.config(menu=menubar)

    #按钮
    contentVar = tkinter.StringVar(root, "hello world")
    contentEntery = tkinter.Entry(root, textvariable=contentVar, state="readonly").place(x=0, y=10, width=160, height=30)
    btn = tkinter.Button(root, text="确认", bg="blue", command=buttonClik).place(x=180, y=10, width=160, height=30)
    #选择框
    checkValue = tkinter.IntVar()
    checklabel = tkinter.Label(root, bg="yellow", text="empty")
    checklabel.place(x=360, y=10, width=160, height=30)
    checkButton = tkinter.Checkbutton(root, text = "选择", variable=checkValue, onvalue=1, offvalue=0, command=checkButtonClik)
    checkButton.place(x=500, y=10, width=160, height=30)
    #画布
    canvas = tkinter.Canvas(root, bg="black")
    canvas.place(x=0, y=50, width=300, height=300)
    img = tkinter.PhotoImage(file="C:/Users/Administrator/Desktop/fengjing.gif")
    canvas.create_image(5, 5, anchor="nw", image=img)
    canvas.create_arc((10, 50, 240, 210), start=10, extent=160, fill="blue")
    canvas.create_line(10, 50, 100, 100, fill = "white")
    canvas.create_oval(50, 50, 100, 100, fill = "red")
    #列表
    listboxValue = tkinter.StringVar(root, "")
    listboxEntry = tkinter.Entry(root, textvariable=listboxValue)
    listboxEntry.place(x=320, y=50, width=80, height=30)
    listboxButton = tkinter.Button(root, text="显示", command=listCheck)
    listboxButton.place(x=410, y = 50, width=80, height=30)
    listboxList = tkinter.StringVar()
    listboxList.set((11,22,33,44,55,66,77,88,99))
    listbox = tkinter.Listbox(root, listvariable=listboxList, selectmode=tkinter.BROWSE)
    for item in["good", "bad", "hello", "bye"]:
        listbox.insert(tkinter.END, item)
    listbox.place(x=320, y=100, width=160, height=250)
    #选择按钮
    radioValue = tkinter.IntVar()
    radioValue.set(0)
    radioLabel = tkinter.Label(root, text="选择为 ", bg="red")
    radioLabel.place(x = 540, y = 50, width = 80, height = 30)
    radioButton1 = tkinter.Radiobutton(root,
                                      text="Radio Button A",
                                      variable=radioValue,
                                      value=1,
                                      command=radio_function)
    radioButton1.place(x = 480, y = 100, width = 160, height = 30)
    radioButton2 = tkinter.Radiobutton(root,
                                      text="Radio Button B",
                                      variable=radioValue,
                                      value=2,
                                      command=radio_function)
    radioButton2.place(x=480, y=150, width=160, height=30)
    radioButton3 = tkinter.Radiobutton(root,
                                      text="Radio Button C",
                                      variable=radioValue,
                                      value=3,
                                      command=radio_function)
    radioButton3.place(x=480, y=200, width=160, height=30)

    labelScale = tkinter.Label(root, bg="gray", text="empty")
    labelScale.place(x=480, y=250, width=160, height=30)
    scale = tkinter.Scale(root, label="时间轴", from_=00, to=20, orient=tkinter.HORIZONTAL,
                          length=20, showvalue=0, tickinterval=5, resolution=0, command=print_scale)
    scale.place(x=480, y=290, width=160, height=100)

    frame = tkinter.Frame(root, bg="red", bd=1, relief="sunken")
    frame.place(x=0, y=360, width=140, height=100)
    frame_1 = tkinter.Frame(frame,).place(x=0, y=360, width=140, height=140)
    tkinter.Label(frame_1, text="天王盖地虎").place(x=0, y=380, width=140, height=20)
    tkinter.Label(frame_1, text="宝塔镇河妖").place(x=0, y=420, width=140, height=20)

    tkinter.Button(root, text="选择文件", command=getFileName, bg="blue").place(x=160, y=360, width=140, height=30)
    tkinter.Button(root, text="选择颜色", command=getColor, bg="blue").place(x=160, y=410, width=140, height=30)

    root.mainloop()

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值