将条目小部件中的文本追加到列表中,并在tkinter中显示

时间:2019-04-24 18:34:05

标签: python tkinter tk

我试图让我的程序接受条目小部件的输入,并在按下“打孔”按钮时将其附加到名为“ punches_list”的列表中,以将信息显示到中央Label小部件。

from tkinter import *
from datetime import *




class Applicaton(Frame):

    punches_list = []

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master = master
        self.master.title("Employee Time-Clock")
        self.punch_in = Button(master, text="Punch In", command=self.punch_In)
        self.punch_out = Button(master, text="Punch Out", command=self.punch_Out)
        self.punch_in.grid(column=4, row=0, pady=5)
        self.punch_out.grid(column=3, row=0, pady=5, padx=5)
        self.emp_num = StringVar(master, value=self.punch_in)
        self.ent = Entry(master, textvariable=StringVar)
        self.ent.grid(column=2, row=0)
        self.actives = Label(master, text=self.punches_list)
        self.actives.grid(column=2, row=3)



    def punch_In(self):
        message = "Has punched in at: "
        now = str(datetime.now())
        self.punches_list.append(self.ent.get() + message + now)


    def punch_Out(self):
        message = "Has punched out at: "
        now = str(datetime.now())
        self.punches_list.append(self.ent.get() + message + now)


root = Tk()
root.geometry('300x200')
app = Applicaton(root)
root.mainloop()

1 个答案:

答案 0 :(得分:0)

也许标签太小,为什么不使用列表框?

我做了一些更改。

#!/usr/bin/python3
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
from datetime import *


class Main(ttk.Frame):
    def __init__(self, parent):
        super().__init__()

        self.parent = parent

        self.punches_list = []
        self.ent = tk.StringVar()

        self.init_ui()

    def init_ui(self):


        f = ttk.Frame()
        ttk.Label(f, text = "Entry").pack()
        self.txTest = ttk.Entry(f, textvariable=self.ent).pack()
        self.lstItems = self.get_listbox(f, 40,80)
        w = ttk.Frame()

        ttk.Button(w, text="Punch In",command=self.punch_In).pack()
        ttk.Button(w, text="Punch Out", command=self.punch_Out).pack()
        ttk.Button(w, text="Close", command=self.on_close).pack()

        f.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)
        w.pack(side=tk.RIGHT, fill=tk.BOTH, expand=1)


    def punch_In(self,):
        s = "IN {0:>30} {1}".format(str(datetime.now()), self.ent.get())
        self.set_list(s)

    def punch_Out(self):
        s = "OUT {0:>29} {1}".format(str(datetime.now()), self.ent.get())
        self.set_list(s)

    def set_list(self,s):

        self.punches_list.append(s)

        self.lstItems.delete(0, tk.END)
        for i in self.punches_list:
            self.lstItems.insert(tk.END, i)

    def on_set(self):

        self.check.set(1)

    def on_close(self):
        self.parent.on_exit()

    def get_listbox(self, container, height=None, width=None):


        sb = tk.Scrollbar(container,orient=tk.VERTICAL)

        w = tk.Listbox(container,
                    relief=tk.GROOVE,
                    selectmode=tk.BROWSE,
                    height=height,
                    width=width,
                    background = 'white',
                    font='TkFixedFont',
                    yscrollcommand=sb.set,)

        sb.config(command=w.yview)

        w.pack(side=tk.LEFT,fill=tk.BOTH, expand =1) 
        sb.pack(fill=tk.Y, expand=1)

        return w        

class App(tk.Tk):
    """Start here"""

    def __init__(self):
        super().__init__()

        self.protocol("WM_DELETE_WINDOW", self.on_exit)

        self.set_style()
        self.set_title() 
        Main(self,)     

    def set_style(self):
        self.style = ttk.Style()
        #('winnative', 'clam', 'alt', 'default', 'classic', 'vista', 'xpnative')
        self.style.theme_use("clam")

    def set_title(self):
        s = "{0}".format('Employee Time-Clock')
        self.title(s)

    def on_exit(self):
        """Close all"""
        if messagebox.askokcancel( self.title(), "Do you want to quit?", parent=self):
            self.destroy()               

if __name__ == '__main__':
    app = App()
    app.mainloop()

enter image description here