aiqianqian ·¢±íÓÚ 2019-12-25 10:41

tkinter֮ѧϰ֮·

³ÌÐò¹¦ÄÜ£º
1.µã»÷¹¦Äܼü£»
2.ʵʱ»ØÏÔÊä³ö£»
3.Çå³ýÊý¾Ý¹¦ÄÜ£»
4.δ×öÈÕÖ¾¼Ç¼¡£
ÔËÐÐÈçͼ£º


´úÂëÈçÏ£º²¢Î´×÷³öÓÅ»¯¡£
ÓÅ»¯¿É²Î¿¼Á´½Ó https://www.cnblogs.com/hhh5460/p/5188524.html

#!/usr/bin/python
# -*- coding: UTF-8 -*-


# tkinter_main.py
from tkinter import *
from tkinter import simpledialog, filedialog, messagebox
import operator, time, subprocess, sys, os, threading, datetime
#from PIL import ImageTk, Image

top = Tk()

class Application_Mian(Frame):
    def __init__(self):# Ö÷¿ò¼Ü
      """
      ÔÚGUIÖУ¬Ã¿¸öButton¡¢Label¡¢ÊäÈë¿òµÈ£¬¶¼ÊÇÒ»¸öWidget£¨¿Ø¼þ£©¡£ÔÚtkinterÖÐÓÃFrame±íʾ´°¿Ú£¬FrameÒ²ÊÇÒ»¸öWidget£¬Frame¿ÉÒÔÈÝÄÉÆäËüWidget¡£±¾³ÌÐòÖдÓFrameÀàÅÉÉú³öApplicationÀ࣬À´ÈÝÄɸ÷ÖÖWidget¡£
      ApplicationÀàµÄ¹¹Ô캯Êý __init__(self,master=None)ÓÐÁ½¸ö²ÎÊý£¬self´ú±íApplication±¾Éí£¬£¨ÔÚtkinterÖУ¬Ò»¸öWidget¿ÉÄÜÊôÓÚÁíÒ»¸öWidget£¬ÕâʱµÄÁíÒ»¸öWidget¾ÍÊÇÕâ¸ö¿Ø¼þµÄmaster£©¶ø´°¿ÚûÓÐmaster,ËùÒÔÔÚÕâÀïmaster=None¡£
      geometry('800x430')±íʾ½«´°¿Ú´óСÉèΪ¡°800x430¡±´óС£¬self.createWidgets()ÊÇ´´½¨WidgetsµÄ·½·¨£¬¾ßÌåÔÚºóÃæ¡£
      resizable(0, 0)±íʾ²»ÔÊÐíµ÷½Ú´°¿Ú´óС£¬½«²ÎÊýÉèΪ1Ôò±íʾÔÊÐíµ÷½Ú¡£

      """
      Frame.__init__(self, master=None)
      image_bm = PhotoImage(file='image/background.png')
      self.master.geometry('1024x568')# ÉèÖóÌÐòÆô¶¯Ä¬ÈÏ´óС
      self.master.resizable(width=False, height=False)# ¿í²»¿É±ä, ¸ß¿É±ä,ĬÈÏΪTrue
       # Label(self.master,image=image_bm)
      self.createWidgets()

    def createWidgets(self):
      '''Ôö¼Ó²Ëµ¥
      # tearoffµÄÖµÓÐ0ºÍ1£¬Îª0ʱ±íʾ×Ӳ˵¥²»¶ÀÁ¢³öÀ´¡£)
      # add_cascade£ºÌí¼ÓÏÂÀ­²Ëµ¥
      '''
      self.menuBar = Menu(self.master)##´´½¨²Ëµ¥µÄʵÀý
      self.master.config(menu=self.menuBar,bg='#CDCDB4')# ½«¸ù´°¿ÚµÄ¶¥¼¶²Ëµ¥ÉèÖÃΪmenu
      # ÉèÖò˵¥Ñ¡Ïî
      # ´´½¨Ò»¸öÏÂÀ­²Ëµ¥¡®¹ØÓÚ¡¯£¬Õâ¸ö²Ëµ¥ÊǹÒÔÚmenubar£¨¶¥¼¶²Ëµ¥£©ÉϵÄ
      # tearoffµÄÖµÓÐ0ºÍ1£¬Îª0ʱ±íʾ×Ӳ˵¥²»¶ÀÁ¢³öÀ´¡£
      aboutMenu = Menu(self.menuBar, tearoff=0)
      # ´´½¨Ò»¸öÏÂÀ­²Ëµ¥¡®¹¦ÄÜ¡¯£¬Õâ¸ö²Ëµ¥ÊǹÒÔÚmenubar£¨¶¥¼¶²Ëµ¥£©ÉϵÄ
      moreMenu = Menu(self.menuBar, tearoff=0)
      # ´´½¨Ò»¸öÏÂÀ­²Ëµ¥¡®Ô¤Áô¹¦ÄÜ1-4¡¯£¬Õâ¸ö²Ëµ¥ÊǹÒÔÚmenubar£¨¶¥¼¶²Ëµ¥£©ÉϵÄ
      ReserveMenu1 = Menu(self.menuBar, tearoff=0)
      ReserveMenu2 = Menu(self.menuBar, tearoff=0)
      ReserveMenu3 = Menu(self.menuBar, tearoff=0)
      ReserveMenu4 = Menu(self.menuBar, tearoff=0)
      # add_cascade£ºÌí¼ÓÏÂÀ­²Ëµ¥
      self.menuBar.add_cascade(label='³ÌÐòÔ¤Áô1', menu=ReserveMenu1)
      self.menuBar.add_cascade(label='³ÌÐòÔ¤Áô2', menu=ReserveMenu2)
      self.menuBar.add_cascade(label='³ÌÐòÔ¤Áô3', menu=ReserveMenu3)
      self.menuBar.add_cascade(label='³ÌÐòÔ¤Áô4', menu=ReserveMenu4)
      self.menuBar.add_cascade(label='¹¦ÄÜ', menu=moreMenu)
      self.menuBar.add_cascade(label='°ïÖú', menu=aboutMenu)
      # ÏÂÀ­²Ëµ¥µÄ¾ßÌåÏîÄ¿£¬Ê¹ÓÃadd_command()·½·¨
      aboutMenu.add_command(label='¹ØÓÚ', command=self.AboutMenu)
      aboutMenu.add_command(label='Í˳ö', command=self.QuitMenu)
      moreMenu.add_command(label='ÆäËû', command=self.OtherMenu)
      # Ô¤Áô°´Å¥ÏÂÀ­¿ò
      # ReserveMenu1
      ReserveMenu1.add_command(label='³ÌÐòÔ¤Áô1', command=self.ReserveMenu)
      ReserveMenu1.add_command(label='³ÌÐòÔ¤Áô2', command=self.ReserveMenu)
      ReserveMenu1.add_command(label='³ÌÐòÔ¤Áô3', command=self.ReserveMenu)
      ReserveMenu1.add_command(label='³ÌÐòÔ¤Áô4', command=self.ReserveMenu)
      # ReserveMenu2
      ReserveMenu2.add_command(label='³ÌÐòÔ¤Áô1', command=self.ReserveMenu)
      ReserveMenu2.add_command(label='³ÌÐòÔ¤Áô2', command=self.ReserveMenu)
      ReserveMenu2.add_command(label='³ÌÐòÔ¤Áô3', command=self.ReserveMenu)
      ReserveMenu2.add_command(label='³ÌÐòÔ¤Áô4', command=self.ReserveMenu)
      # ReserveMenu3
      ReserveMenu3.add_command(label='³ÌÐòÔ¤Áô1', command=self.ReserveMenu)
      ReserveMenu3.add_command(label='³ÌÐòÔ¤Áô2', command=self.ReserveMenu)
      ReserveMenu3.add_command(label='³ÌÐòÔ¤Áô3', command=self.ReserveMenu)
      ReserveMenu3.add_command(label='³ÌÐòÔ¤Áô4', command=self.ReserveMenu)
      # ReserveMenu4
      ReserveMenu4.add_command(label='³ÌÐòÔ¤Áô1', command=self.ReserveMenu)
      ReserveMenu4.add_command(label='³ÌÐòÔ¤Áô2', command=self.ReserveMenu)
      ReserveMenu4.add_command(label='³ÌÐòÔ¤Áô3', command=self.ReserveMenu)
      ReserveMenu4.add_command(label='³ÌÐòÔ¤Áô4', command=self.ReserveMenu)

      # ÏÔʾÃæ°å
      self.result = StringVar()
      self.result.set('Ö´Ðнá¹û')
      self.result2 = StringVar()
      self.result2.set('Ö´ÐлØÏÔ½çÃæ')

      # ÏÔʾÃæ°åÉèÖÃ
      #Ö´Ðнá¹û
      # #Ö´ÐлØÏÔ½çÃæ¼Ç¼
      # ---------------------------------------------------------------------------------------------
      self.listbox1=Listbox(self.master,font=('΢ÈíÑźÚ', 10), bg='#CDCDB4', bd='0', fg='#828282',state=NORMAL)
      #ÏÂÀ­Öù
      self.scrollbar_listbox1 = Scrollbar(self.listbox1)
      self.scrollbar_listbox1.pack(side=RIGHT, fill=Y)
      self.listbox1['yscrollcommand'] = self.scrollbar_listbox1.set

      self.listbox1.place( y=45, width=600, height=520)
      self.scrollbar_listbox1['command'] = self.listbox1.yview
      #---------------------------------------------------------------------------------------------
      # ÈÕÖ¾¼Ç¼
      #---------------------------------------------------------------------------------------------
      self.listbox2=Listbox(self.master,font=('΢ÈíÑźÚ', 10), bg='#CDCDB4', bd='0', fg='#828282',state=NORMAL)
      #ÏÂÀ­Öù
      self.scrollbar_listbox2 = Scrollbar(self.listbox2)
      self.scrollbar_listbox2.pack(side=RIGHT, fill=Y)
      self.listbox2['yscrollcommand'] = self.scrollbar_listbox2.set
      self.listbox2.place(x=610, y=45, width=390, height=520)
      self.scrollbar_listbox2['command'] = self.listbox2.yview
      #---------------------------------------------------------------------------------------------

      """
      # ²»´øÀ¨ºÅµ÷ÓõĽá¹û£º<function bracket at 0x0000000004DD0B38>,aÊÇÕû¸öº¯ÊýÌ壬ÊÇÒ»¸öº¯Êý¶ÔÏ󣬲»ÐëµÈ¸Ãº¯ÊýÖ´ÐÐÍê³É
      # ´øÀ¨ºÅµ÷ÓõĽá¹û£º6 ,bÊǺ¯ÊýÖ´Ðк󷵻صÄÖµ6,ÐëµÈ¸Ãº¯ÊýÖ´ÐÐÍê³ÉµÄ½á¹û
      """
      self.Button1 = Button(self.master, text='¹¦Äܼü1', font=('΢ÈíÑźÚ', 10), fg=('#4F4F4F'), bg=('#FFFFF0'), bd=2,
                              command=lambda: self.thread_it( self.pressNum1), relief='raised')
      self.Button1.place(x=10, y=10, width=60, height=30)

      self.Button1 = Button(self.master, text='¹¦Äܼü2', font=('΢ÈíÑźÚ', 10), fg=('#4F4F4F'), bg=('#FFFFF0'), bd=2, command=lambda: self.thread_it(self.pressNum2), relief='raised')
      self.Button1.place(x=75, y=10, width=60, height=30)

      self.Button1 = Button(self.master, text='¹¦Äܼü3', font=('΢ÈíÑźÚ', 10), fg=('#4F4F4F'), bg=('#FFFFF0'), bd=2,command=lambda: self.thread_it(self.pressNum3), relief='raised')
      self.Button1.place(x=140, y=10, width=60, height=30)

      self.Button1 = Button(self.master, text='¹¦Äܼü4', font=('΢ÈíÑźÚ', 10), fg=('#4F4F4F'), bg=('#FFFFF0'), bd=2,command=lambda: self.thread_it(self.pressNum4), relief='raised')
      self.Button1.place(x=205, y=10, width=60, height=30)

      self.Button1 = Button(self.master, text='¹¦Äܼü5', font=('΢ÈíÑźÚ', 10), fg=('#4F4F4F'), bg=('#FFFFF0'), bd=2,command=lambda: self.thread_it(self.pressNum5), relief='sunken')
      self.Button1.place(x=270, y=10, width=60, height=30)

      # µÚ¶þÅŹ¦Äܼü
      self.Button1 = Button(self.master, text='¹¦Äܼü6', font=('΢ÈíÑźÚ', 10), fg=('#4F4F4F'), bg=('#FFFFF0'), bd=2,command=lambda: self.thread_it(self.pressNum6), relief='sunken')
      self.Button1.place(x=335, y=10, width=60, height=30)

      self.Button1 = Button(self.master, text='¹¦Äܼü7', font=('΢ÈíÑźÚ', 10), fg=('#4F4F4F'), bg=('#FFFFF0'), bd=2,command=lambda: self.thread_it(self.pressNum7), relief='groove')
      self.Button1.place(x=400, y=10, width=60, height=30)

      self.Button1 = Button(self.master, text='¹¦Äܼü8', font=('΢ÈíÑźÚ', 10), fg=('#4F4F4F'), bg=('#FFFFF0'), bd=2,command=lambda: self.thread_it(self.pressNum8), relief='groove')
      self.Button1.place(x=465, y=10, width=60, height=30)

      self.Button1 = Button(self.master, text='¹¦Äܼü9', font=('΢ÈíÑźÚ', 10), fg=('#4F4F4F'), bg=('#FFFFF0'), bd=2,command=lambda:self.thread_it(self.pressNum9), relief='ridge')
      self.Button1.place(x=530, y=10, width=60, height=30)

      self.Button1 = Button(self.master, text='Çå³ýÊý¾Ý', font=('΢ÈíÑźÚ', 10), fg=('#4F4F4F'), bg=('#FFFFF0'), bd=2,command=lambda: self.thread_it(self.Clr), relief='ridge')
      self.Button1.place(x=605, y=10, width=60, height=30)

      self.Clock_func = Label(self.master,text="%s" % (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')),bg='#CDCDB4')
      self.Clock_func.place(x=700, y=10, width=300, height=30)
      self.Clock_func.after(1000, self.Clock)
    #-----------------------------------------------------------------
    def AboutMenu(self):
      messagebox.showinfo('¹ØÓÚ', 'xxxxxx¹¤¾ßºÏ¼¯ \n \nQQ:xxxxxxx\n')

    def QuitMenu(self):
      messagebox.showinfo('Í˳ö', ' ÔÝʱûÓÐÍ˳ö¹¦ÄÜ\n emmmm...')

    def OtherMenu(self):
      messagebox.showinfo('¹¦ÄÜ', ' ÔÝʱûÓÐÆäËû¹¦ÄÜ\n emmmm...')

    def ReserveMenu(self):
      messagebox.showinfo('³ÌÐòÔ¤Áômassage',
                            """ÔÝʱûÓÐÆäËû¹¦ÄÜ\n emmmm...\nhttp://www.sevenday.cf """)

    #Êý¾ÝÔ¤´æ´¦Àí£¬°´¼üºó¶þ´Î²Ù×÷
    def Data_pre_processing(self,PressMenu):
      current_time = self.get_current_time()
      # for i in range(10):
      logmsg_in = str(current_time) + " " + PressMenu + ("Ö´ÐÐÍê³É")+"\n"# »»ÐÐ
      #print("logggggggs Data_pre_processing ",logmsg_in)
      self.listbox2.insert(END, logmsg_in)

    #ʵʱ»ØÏÔ·½·¨
    def Echo_in_real_time(self, bat_func):
      current_time = self.get_current_time()
      pi = subprocess.Popen(bat_func, shell=True, stdout=subprocess.PIPE)
      for i in iter(pi.stdout.readline, 'b'):
            time.sleep(0.3)
            if not i:
                break
            logmsg_in = str(current_time) + " " + str(i.decode('gbk')) + "\n"# »»ÐÐ
            self.listbox1.insert(END, logmsg_in)

    #ʱ¼ä´Á
    def get_current_time(self):
      current_time = time.strftime('%Y-%m-%d %H:%M', time.localtime(time.time()))
      return current_time

    def Clock(self):
      self.Clock_func["text"] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
      self.Clock_func.after(1000, self.Clock)
    #---------------------------------------------------------------------------------------
    # ¹¦Äܼü1
    def pressNum1(self):
      #messagebox.showinfo('¹¦ÄÜ1', ' ÕýÔÚÖ´ÐвÙ×÷...')
      self.Echo_in_real_time("%cd%/bat/text1.bat")
      self.Data_pre_processing("°´¼ü1")

    # ¹¦Äܼü2
    def pressNum2(self):
      #messagebox.showinfo('¹¦ÄÜ2', ' ÕýÔÚÖ´ÐвÙ×÷...')
      self.Echo_in_real_time("%cd%/bat/2.seting_IE_ActiveX_open.bat")
      self.Data_pre_processing("°´¼ü2")

    # ¹¦Äܼü3
    def pressNum3(self):
      #messagebox.showinfo('¹¦ÄÜ3', ' ÔÝʱûÓÐÆäËû¹¦ÄÜ\n emmmm...')
      self.Data_pre_processing("°´¼ü3")

    # ¹¦Äܼü4
    def pressNum4(self):
      #messagebox.showinfo('¹¦ÄÜ4', ' ÔÝʱûÓÐÆäËû¹¦ÄÜ\n emmmm...')
      self.Data_pre_processing("°´¼ü4")

    # ¹¦Äܼü5
    def pressNum5(self):
      #messagebox.showinfo('¹¦ÄÜ5', ' ÔÝʱûÓÐÆäËû¹¦ÄÜ\n emmmm...')
      self.Data_pre_processing("°´¼ü5")

    # ¹¦Äܼü6
    def pressNum6(self):
      #messagebox.showinfo('¹¦ÄÜ6', ' ÔÝʱûÓÐÆäËû¹¦ÄÜ\n emmmm...')
      self.Data_pre_processing("°´¼ü6")

    # ¹¦Äܼü7
    def pressNum7(self):
      #messagebox.showinfo('¹¦ÄÜ7', ' ÔÝʱûÓÐÆäËû¹¦ÄÜ\n emmmm...')
      self.Data_pre_processing("°´¼ü7")

    # ¹¦Äܼü8
    def pressNum8(self):
       # messagebox.showinfo('¹¦ÄÜ8', ' ÔÝʱûÓÐÆäËû¹¦ÄÜ\n emmmm...')
      self.Data_pre_processing("°´¼ü8")

    #¹¦Äܼü9
    def pressNum9(self):
       # messagebox.showinfo('¹¦ÄÜ9', ' ÔÝʱûÓÐÆäËû¹¦ÄÜ\n emmmm...')
      self.Data_pre_processing("°´¼ü9")

    #Çå³ý¼Ç¼
    def Clr(self):
      # app.result_set('µÈ´ý³ÌÐòÊä³ö...')# ½«ÔËËã½á¹ûÏÔʾµ½ÆÁÄ»1
      # app.result2_set('¹éÁãÖÐ...')# ½«ÔËËã¹ý³ÌÏÔʾµ½ÆÁÄ»2
      # lists.clear()# Çå¿ÕÁбíÄÚÈÝ
      # messagebox.showinfo('Zeroing...', 'Zeroing... \n Please wait....')
      time.sleep(0.3)
      self.listbox1.delete(0,END)
      # self.listbox2.set('')
      self.Data_pre_processing("Çå³ý¼Ç¼")
      messagebox.showinfo('Çå³ý¼Ç¼', '»ØÏÔ½çÃæÇåÀí³É¹¦\n Çë¼ÌÐøʹÓÃ....')
    # ´ò°ü½øỊ̈߳¨ºÄʱµÄ²Ù×÷£©
    @staticmethod
    def thread_it(func, *args):
      t = threading.Thread(target=func, args=args)
      t.setDaemon(True)# ÊØ»¤--¾ÍËãÖ÷½çÃæ¹Ø±Õ£¬Ïß³ÌÒ²»áÁôÊغǫ́ÔËÐУ¨²»¶Ô!£©
      t.start()# Æô¶¯
      # t.join()   # ×èÈû--»á¿¨ËÀ½çÃ棡


# ʵÀý»¯Application()Àà
app = Application_Mian()
# ÉèÖô°¿Ú±êÌâ:
app.master.title('xxxxxx¹¤¾ßºÏ¼¯')
# Ö÷ÏûϢѭ»·:
app.mainloop()

1732501894 ·¢±íÓÚ 2019-12-25 11:42

±¾Ìû×îºóÓÉ 1732501894 ÓÚ 2019-12-25 11:48 ±à¼­

ÒѾ­Æú¿ÓתPyQt5ÁË
Ô­Òò£º1.»­Ãæ²»ÈçQt5ϸÄå
2.¸öÈ˸оõ¹¦ÄÜʵÏÖ²»Èçpyqt5Ç¿´ó£¬ËäÈ»tkinter¼òµ¥Ò×ѧ
3.ʹÓÃÎĵµÕæµÄͦÉÙ...

Tairraos ·¢±íÓÚ 2019-12-27 17:54

1732501894 ·¢±íÓÚ 2019-12-25 11:42
ÒѾ­Æú¿ÓתPyQt5ÁË
Ô­Òò£º1.»­Ãæ²»ÈçQt5ϸÄå
2.¸öÈ˸оõ¹¦ÄÜʵÏÖ²»Èçpyqt5Ç¿´ó£¬ËäÈ»tkinter¼òµ¥Ò×ѧ


QTÐèÒªpip°²×°500MµÄÒÀÀµ°ü¡£´øÓÐQTµÄ´úÂë´ò°ü³É¿ÉÖ´ÐÐÎļþÖÁÉÙ100M´óС¡£

TKÇáÁ¿¼¶£¬ºÜ¶àС¹¤¾ßÄþ¿É·ÅÆúUI¸ß´óÉÏÑ¡ÔñÓµ±§¸üСµÄ·¢²¼³ß´ç

vinsta ·¢±íÓÚ 2019-12-25 11:33

ѧϰһÏ£¡

Ò¶Ó£·ã ·¢±íÓÚ 2019-12-27 16:29

ÕýÔÚѧPython¡£

aiqianqian ·¢±íÓÚ 2019-12-29 21:02

13419622223 ·¢±íÓÚ 2019-12-26 13:34
tkinter ѧϰÁË£¡£¡£¡

¸öÈËÓÐÁô´æ¸÷ÖÖtkinterÎĵµ£¬ÐèÒªµÄDD

shdfjhg ·¢±íÓÚ 2019-12-29 22:54

¸Ðл·ÖÏí£¬ÓÐÓÃ

zhengkejie ·¢±íÓÚ 2020-4-12 17:24

aiqianqian ·¢±íÓÚ 2019-12-29 21:02
¸öÈËÓÐÁô´æ¸÷ÖÖtkinterÎĵµ£¬ÐèÒªµÄDD

´óÐֵܣ¬tkinterµÄÎĵµ·ÖÏíһϡ£

zhengkejie ·¢±íÓÚ 2020-4-12 17:25

zhengkejie ·¢±íÓÚ 2020-4-12 17:24
´óÐֵܣ¬tkinterµÄÎĵµ·ÖÏíһϡ£

Ä¿Ç°ÕýÔÚÕÒÀàËÆÓÚQQ±ßÔµÎü¸½µÄ¹¦ÄÜ£¬Ïë×ö¸ö×ÀÃæС¹¤¾ß£¬¿ÉÒÔÐü¸¡ÒþÉíµÄ¡£

ϲ»¶ÄãºÍËáÄÌ ·¢±íÓÚ 2020-5-9 10:06

aiqianqian ·¢±íÓÚ 2019-12-29 21:02
¸öÈËÓÐÁô´æ¸÷ÖÖtkinterÎĵµ£¬ÐèÒªµÄDD

ÀϸçÇótkinter·ÖÏí
Ò³: [1] 2
²é¿´ÍêÕû°æ±¾: tkinter֮ѧϰ֮·