是否可以从 tkinter 窗口创建框架?

时间:2021-03-02 20:24:27

标签: python python-3.x tkinter

我在网上找了很长时间的模拟时钟,确实有效。现在我找到了一个时钟并做了一些调整。如果您按原样运行代码,它会在新窗口中打开,但可以将此时钟保存在一个框架中。我已经尝试了很长时间,但我做不到。现在我的问题是如何以最简单的方式做到这一点? 这是我的代码:

import tkinter as Tkinter

import math
import time

from PIL import Image





class create_clock(Tkinter.Tk):
    def __init__(self, size):
        Tkinter.Tk.__init__(self)
        image = Image.open("clock-v2.gif")
        image = image.resize((size, size), Image.ANTIALIAS)
        image.save(fp="data/Widgets/Clock/Clock_sizes/acc.png")

        self.size = size
        self.x = size/2  # Center Point x
        self.y = size/2   # Center Point
        self.length = (size/2)*0.75  # Stick Length
        self.creating_all_function_trigger()

    # Creating Trigger For Other Functions
    def creating_all_function_trigger(self):
        self.create_canvas_for_shapes()
        self.creating_background_()
        self.creating_sticks()
        return

    # Creating Background
    def creating_background_(self):
        self.image = Tkinter.PhotoImage(file ='data/Widgets/Clock/Clock_sizes/acc.png')
        self.canvas.create_image(self.size/2, self.size/2, image=self.image,)

        return

    # creating Canvas
    def create_canvas_for_shapes(self):
        self.canvas = Tkinter.Canvas(self,  bg ='#1E1E1E')
        self.canvas.pack(expand='yes', fill='both')
        return


    def creating_sticks(self):
        self.sticks = []
        for i in range(3):
            if i == 1:
                store = self.canvas.create_line(self.x, self.y, self.x + self.length*1.066666666666667, self.y + self.length*1.066666666666667, width=5, fill='#6D6D6D')
                self.sticks.append(store)

            elif i == 0:
                at = self.length -100
                store = self.canvas.create_line(self.x, self.y, self.x + at, self.y + at, width=5,
                                                fill='#6D6D6D')
                self.sticks.append(store)
            elif i == 2:
                store = self.canvas.create_line(self.x, self.y, self.x + self.length*1.066666666666667, self.y + self.length*1.066666666666667, width=1.5,
                                                fill='orange')
                self.sticks.append(store)



        return

    # Function Need Regular Update
    def update_class(self):
        now = time.localtime()

        t = time.strptime(str(now.tm_hour), "%H")
        hour = int(time.strftime("%I", t)) * 5 + (now.tm_min*0.0833333333333333)




        now = (hour, now.tm_min, now.tm_sec)


        for n, i in enumerate(now):
            x, y = self.canvas.coords(self.sticks[n])[0:2]
            cr = [x, y]

            if n == 1:
                cr.append((self.length +0)* math.cos(math.radians(i * 6) - math.radians(90)) + self.x)
                cr.append((self.length + 0) * math.sin(math.radians(i * 6) - math.radians(90)) + self.y)
            elif n == 0:
                cr.append((self.length *0.6666) * math.cos(math.radians(i * 6) - math.radians(90)) + self.x)
                cr.append((self.length *0.6666) * math.sin(math.radians(i * 6) - math.radians(90)) + self.y)
            elif n == 2:
                cr.append((self.length*1.066666666666667) * math.cos(math.radians(i * 6) - math.radians(90)) + self.x)
                cr.append((self.length*1.066666666666667) * math.sin(math.radians(i * 6) - math.radians(90)) + self.y)




            self.canvas.coords(self.sticks[n], tuple(cr))
        return








root = create_clock(size=200
)

while True:
    root.update()
    root.update_idletasks()
    root.update_class()

1 个答案:

答案 0 :(得分:0)

您可以继承 Frame 而不是 Tk。然后,您需要调用 packplacegrid 将框架添加到窗口。

class create_clock(Tkinter.Frame):
    def __init__(self, size):
        Tkinter.Frame.__init__(self)
        ...

root = Tkinter.Tk()
clock = create_clock(size=200)
clock.pack(fill="both", expand=True)

我还建议调用类 Clock 而不是 create_clock,因为您正在创建时钟对象而不是函数。

需要修改代码中的最后一个块。像您这样使用 while 循环效率不高,应该避免。相反,您可以像这样编写最后一个块:

def update_every_second():
    clock.update_class()
    clock.after(1000, update_every_second)

update_every_second()
root.mainloop()