python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > python tkinter

python tkinter控件布局项目实例

作者:changfan

这篇文章主要介绍了python tkinter控件布局项目实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

这篇文章主要介绍了python tkinter控件布局项目实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

代码部分:

from tkinter import *
import tkinter.messagebox as messagebox

class Tkdemo():
  def __init__(self):
    master = Tk()
    master.title("missWjz")
    master.geometry('800x800')
    #创建菜单栏
    menubar = Menu(master)
    master.config(menu=menubar)

    #创建下拉菜单栏
    filemenu = Menu(menubar,tearoff=0)

    menubar.add_cascade(label="文件",menu=filemenu)
    filemenu.add_command(label="新建",command=self.newfile)
    filemenu.add_command(label="打开",command=self.openfile)
    filemenu.add_command(label="保存",command=self.savefile)
    #增加分割线
    filemenu.add_separator()
    filemenu.add_command(label="关闭填写",command=master.quit)

    #Label(文字)
    title = Label(master,text="这是一份Tkinter的问卷调查",font=15,bg='white',fg='red')
    title.pack()

    #Frame 将问题1放在frame_1中
    frame_1 = Frame(master)
    frame_1.pack(fill=X)

    #问题1
    label_1 = Label(frame_1,text="1、我们第一次见面的地方?")
    label_1.grid(row=1,column=0)

    #Entry 问题一的输入框
    self.name = StringVar()
    entryname = Entry(frame_1,textvariable=self.name)
    #通过grid方法定位
    entryname.grid(row=1,column=1)

    #Button 按钮
    getname = Button(frame_1,text="点击确认",command=self.getname)
    getname.grid(row=1,column=3)

    #Frame 将问题二放在frame_2中
    frame_2 = Frame(master)
    frame_2.pack(fill=X)

    #Label 问题二
    label_2 = Label(frame_2,text="2、我记得你生日")
    label_2.grid(row=1,column=0)

    #Radiobutton 选择按钮
    self.sex = StringVar()
    sex_male = Radiobutton(frame_2,text='记得',fg='blue',variable=self.sex,value='记得',command=self.getsex)
    sex_male.grid(row=1,column=2)
    sex_female = Radiobutton(frame_2,text='不可能',fg='red',variable=self.sex,value="不可能",command=self.getsex)
    sex_female.grid(row=1,column=4)

    #Frame 问题3 放在frame_3中
    frame_3 = Frame(master)
    frame_3.pack(fill=X)

    #Label 问题3
    label_3 = Label(frame_3,text="3、你对我的重要性:")
    label_3.grid(row=1,column=0)

    #Scale 滑动条
    self.age = Scale(frame_3,from_=0,to=100,orient=HORIZONTAL,resolution=1)
    self.age.grid(row=1,column=1)

    #Button 按钮
    getage = Button(frame_3,text='点我确认',command=self.getage)
    getage.grid(row=1,column=2)

    #问题4 置于 Frame_4 中
    frame_4 = Frame(master)
    frame_4.pack(fill=X)

    #问题四
    label_4 = Label(frame_4,text="4、去除一段过去:")
    label_4.grid(row=1,column=0)

    #列表
    self.listbox = Listbox(frame_4)
    self.listbox.grid(row=1,column=1)
    for item in ["第一次看电影","第一次拍照留影","第一次一起吃饭","第一次一起出去玩"]:
      self.listbox.insert(END,item)

    #s删除按钮
    DELE = Button(frame_4,text="删除",command=lambda listbox=self.listbox: listbox.delete(ANCHOR))
    DELE.grid(row=1,column=1)

    #确认按钮
    review = Button(frame_4,text='点我确认',command = self.review)
    review.grid(row=1,column=2)

    #问题五
    frame_5 = Frame(master)
    frame_5.pack(fill=X)

    label_5 = Label(frame_5,text='5、选择一个图案:')
    label_5.grid(row=1,column=0)
    self.canvas = Canvas(frame_5,width=200,height=200,bg='white')
    self.canvas.grid(row=1,column=1)

    self.pattern = StringVar()

    #图案选择按钮
    btn_first = Button(frame_5,text = "第一个",command =self.First_display)
    btn_second = Button(frame_5,text = "第二个",command =self.Second_dispaly)
    btn_third = Button(frame_5,text = "第三个",command =self.Third_display)
    btString = Button(frame_5, text="确定", command=self.displayString)
    btn_first.grid(row=2,column=2)
    btn_second.grid(row=2,column=3)
    btn_third.grid(row=2,column=4)
    btString.grid(row=2,column=5)

    #问题六 Frame_6
    frame_6 = Frame(master)
    frame_6.pack(fill=X)

    label_6 = Label(frame_6,text="6、在一起的天数:")
    label_6.grid(row=1,column=0)

    #(Spinbox)
    self.number = Spinbox(frame_6,from_=0,to=200)
    self.number.grid(row=1,column=1)

    #确定按钮
    number = Button(frame_6,text="确定",command=self.getnumber)
    number.grid(row=1,column=2)

    #问题7 frame_7
    frame_7 = Frame(master)
    frame_7.pack(fill=X)

    #问题
    label_7 = Label(frame_7,text="7、印象最深的一天:")
    label_7.grid(row=1,column=0)

    #滚轮
    scrollbar = Scrollbar(frame_7)
    scrollbar.grid(row=1,column=1)

    #列表
    self.listbox2 = Listbox(frame_7,height=5,yscrollcommand=scrollbar.set)
    for i in range(99):
      self.listbox2.insert(END,str(i))
    self.listbox2.grid(row=1,column=1)

    #确定按钮
    star = Button(frame_7,text="确定",command=self.getstar)
    star.grid(row=2,column=1)

    #问题8
    frame_8 = Frame(master)
    frame_8.pack(fill=X)

    self.agree = StringVar()
    #Checkbutton 勾选按钮
    agree = Checkbutton(frame_8,text="我确定此问卷信息准确无误",variable=self.agree,offvalue='不确定',command=self.getagree)
    agree.grid(row=1,column=1)

    #问题9 Frame_9
    frame_9 = Frame(master)
    frame_9.pack()
    submit = Button(frame_9,text="提交",command=self.allsubmit)
    submit.grid(row=1,column=1)

    #问题十
    frame_10 = Frame(master)
    frame_10.pack()
    #容器框
    self.group = LabelFrame(frame_10,text="特别鸣谢",padx=5,pady=5)
    self.group.grid()
    w = Label(self.group,text="本问卷由衷感谢:https://github.com/James95107/Tkinter/blob/master")
    w.pack()

    master.mainloop()




  def getname(self):
    name = self.name.get()
    print(name)

  def getsex(self):
    sex = self.sex.get()
    print(sex)

  def getage(self):
    print(self.age.get())

  def review(self):
    print(self.listbox.get(0, END))

  def getstar(self):
    print(self.listbox2.get(ACTIVE))

  def displayString(self):
    self.canvas.create_text(60, 40, text="您真棒!!!", font="Tine 10 bold underline", tags="string")
    print(self.pattern)

  def getnumber(self):
    print(self.getnumber())

  def getagree(self):
    print(self.agree.get())
  #打印心形
  def First_display(self):
    self.canvas.create_arc((5, 5, 85, 85),
           width=2,
           outline="red",
           start=0,
           extent=180,
           style=ARC)

    self.canvas.create_arc((85, 5, 165, 85),
           width=2,
           outline="red",
           start=0,
           extent=180,
           style=ARC)

    self.canvas.create_arc((5, -45, 165, 125),
           width=2,
           outline="red",
           start=180,
           extent=180,
           style=ARC)
    self.pattern = 'First'
  #打印丘比特心形
  def Second_dispaly(self):
    self.canvas.create_line((0, 55, 200, 55),
            width=2,
            fill='red',
            arrow=LAST,
            arrowshape=(20, 40, 10)
            )
    self.canvas.create_arc((5, 5, 85, 85),
           width=2,
           outline="red",
           start=0,
           extent=180,
           style=ARC)

    self.canvas.create_arc((85, 5, 165, 85),
           width=2,
           outline="red",
           start=0,
           extent=180,
           style=ARC)

    self.canvas.create_arc((5, -45, 165, 125),
           width=2,
           outline="red",
           start=180,
           extent=180,
           style=ARC)
    self.pattern = 'Second'
  #打印爱心
  def Third_display(self):
    self.canvas.create_text((90, 70), text="武峻竹",
            font=('微软雅黑', 20, 'bold'),
            fill='red'
            )
    self.canvas.create_arc((5, 5, 85, 85),
           width=2,
           outline="red",
           start=0,
           extent=180,
           style=ARC)

    self.canvas.create_arc((85, 5, 165, 85),
           width=2,
           outline="red",
           start=0,
           extent=180,
           style=ARC)

    self.canvas.create_arc((5, -45, 165, 125),
           width=2,
           outline="red",
           start=180,
           extent=180,
           style=ARC)
    self.pattern = 'Third'

  #属性
  #文件栏
  def newfile(self):
    self.file = open(r"wujunzhu.txt",'w')
    self.file.close()
    messagebox.showinfo("创建文件","您已经成功创建个人资料文档") #显示对话框

  def openfile(self):
    f = open(r"wujunzhu.txt",'r')
    try:
      f_read = f.read()
      print(f_read)
    finally:
      f.close()

  def savefile(self):
    messagebox.showwarning("保存文件","提交即保存!")

  def allsubmit(self):
    with open('D:cpwords.txt','w',encoding='utf-8') as f:
      f.write('我们第一见面的地方是:')
      f.write(self.name.get())
      f.write('\n我记得你生日:')
      f.write(self.sex.get())
      f.write('\n你对我的重要性:')
      f.write(str(self.age.get()))
      f.write('\n要去除一段过去是:')
      for i in self.listbox.get(0, END):
        f.write(i)
        f.write(" ,")
      f.write('\n在一起的天数是:')
      f.write(self.number.get())
      f.write('\n印象最深的一天是:')
      f.write(self.listbox2.get(ACTIVE))
      f.write('\n')
      f.write(self.agree.get())
      f.write('本调查问卷的真实性')
    messagebox.showinfo('Success', '恭喜您已成功提交 ')  # 显示对话框

Tkdemo()

执行结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文