как создать окно вывода со значениями времени, используя tkinter - PullRequest
0 голосов
/ 08 ноября 2018

Я пытаюсь создать графический интерфейс, который будет отображать текущее местное время и выводить время при нажатии кнопки «Круг».До сих пор я создал часы и кнопки, но не могу понять, как создать окно вывода в окне, которое будет отображать метку времени при нажатии кнопки.У меня очень мало опыта программирования, поэтому любая помощь будет признательна!

Я приложил то, что у меня есть:

import tkinter
import time
class Clock(tkinter.Label):
    def __init__(self, parent=None, seconds=True, colon=False):
        tkinter.Label.__init__(self, parent)

        self.display_seconds = seconds
        if self.display_seconds:
            self.time = time.strftime('%I:%M:%S')
        else:
            self.time = time.strftime('%I:%M %p').lstrip('0')
        self.display_time = self.time
        self.configure(text=self.display_time)

        if colon:
            self.blink_colon()

        self.after(200, self.tick)

    def tick(self):
        if self.display_seconds:
            new_time = time.strftime('%I:%M:%S')
        else:
            new_time = time.strftime('%I:%M %p').lstrip('0')
        if new_time != self.time:
            self.time = new_time
            self.display_time = self.time
            self.config(text=self.display_time)
        self.after(200, self.tick)

def timestamp():
    print(time.strftime("%I:%M:%S"))


if __name__ == "__main__":

    window = tkinter.Tk()
    frame = tkinter.Frame(window, width=800, height=800)
    frame.pack()

    tkinter.Label(frame, text="Current time: ").pack()

    clock1 = Clock(frame)
    clock1.pack()
    clock1.configure(bg='white', fg='black', font=("helvetica", 65))

    tkinter.Label(frame, text=" ").pack()

    b = tkinter.Button(frame, text='Quit', command=quit)
    b.pack(side=tkinter.RIGHT)
    b2 = tkinter.Button(frame, text='Lap', command=timestamp)
    b2.pack(side=tkinter.LEFT)

    window.mainloop()

Мне нужна помощь, чтобы создать окно вывода в окне, которое будетраспечатайте время, когда нажата кнопка «Круг».

1 Ответ

0 голосов
/ 08 ноября 2018

Вы можете создать ScrolledText и вставить в него с помощью команды insert("end", value) при каждом нажатии кнопки. Вот расширенный код.

import tkinter
import time
from tkinter import scrolledtext
class Clock(tkinter.Label):
    def __init__(self, parent=None, seconds=True, colon=False):
        tkinter.Label.__init__(self, parent)

        self.display_seconds = seconds
        if self.display_seconds:
            self.time = time.strftime('%I:%M:%S')
        else:
            self.time = time.strftime('%I:%M %p').lstrip('0')
        self.display_time = self.time
        self.configure(text=self.display_time)

        if colon:
            self.blink_colon()

        self.after(200, self.tick)

    def tick(self):
        if self.display_seconds:
            new_time = time.strftime('%I:%M:%S')
        else:
            new_time = time.strftime('%I:%M %p').lstrip('0')
        if new_time != self.time:
            self.time = new_time
            self.display_time = self.time
            self.config(text=self.display_time)
        self.after(200, self.tick)

def timestamp():
    print(time.strftime("%I:%M:%S"))


if __name__ == "__main__":

    window = tkinter.Tk()
    frame = tkinter.Frame(window, width=800, height=800)
    frame.pack()

    tkinter.Label(frame, text="Current time: ").pack()

    text = scrolledtext.ScrolledText(frame, height=10) ##
    text.pack() ##

    clock1 = Clock(frame)
    clock1.pack()
    clock1.configure(bg='white', fg='black', font=("helvetica", 65))

    tkinter.Label(frame, text=" ").pack()

    b = tkinter.Button(frame, text='Quit', command=quit)
    b.pack(side=tkinter.RIGHT)
    b2 = tkinter.Button(frame, text='Lap', command=lambda :text.insert("end", time.strftime("%I:%M:%S")+'\n')) ##
    b2.pack(side=tkinter.LEFT)

    window.mainloop()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...