为什么在Windows和Linux中运行时,相同的程序看起来有所不同?

时间:2017-04-06 23:48:39

标签: python linux windows tkinter

我编写了一个简单的Python应用程序来测量某个事件的时间。一切正常,但它在两个不同的操作系统上看起来完全不同的事实让我有点头疼。我可以做点什么,为什么会这样?我的意思是,最后,代码是相同的。

# str8.py
#   Program to measure time from a certain event

from tkinter import *
from datetime import datetime

class FormatLabel(Label):

    # A new type of Label widget that allows both text and textvariable

    def __init__(self, master = None, **kwargs):

        self.textvariable = kwargs.pop('textvariable', StringVar(master))
        self.text = kwargs.pop('text', '{}')
        self.textvariable.trace('w', self.update_text)

        Label.__init__(self, master, **kwargs)

    def update_text(self, *args):

        self.config(text = self.text.format(self.textvariable.get()))

class App(Frame):

    def __init__(self, master = None, **kwargs):

        Frame.__init__(self, master, **kwargs)

        lbl = Label(self, text = 'You have been STR8 for:\n', font = 'Verdana 8 bold')
        lbl.grid(row=0, sticky=W)

        self.counters = dict()
        measurements = [ 'Years', 'Weeks', 'Days', 'Hours', 'Minutes', 'Seconds' ]

        for i, measurement in enumerate(measurements):

            self.counters[measurement] = DoubleVar()

            lbl = FormatLabel(self,
                              text = measurement + ": {:.2f}", # set the rounding here
                              textvariable = self.counters[measurement],
                              font = 'Verdana 8')
            lbl.grid(row = i + 1, column = 0, sticky = W)

            self.counters[measurement].set(0)

        btn = Button(self,
                     text = 'EXIT',
                     font = 'Verdana 8',
                     height = 1,
                     width = 19,
                     command = quit)
        btn.grid(row = 7, column = 0)

        self.increment()

    def increment(self):

        event = datetime(2017, 4, 4, 0, 0, 0)
        today = datetime.now()

        str8 = (today - event).total_seconds()

        self.counters['Minutes'].set(str8 / 60.)
        self.counters['Hours'].set(str8 / 3600.)
        self.counters['Seconds'].set(str8)
        self.counters['Days'].set(str8 / (3600. * 24))
        self.counters['Weeks'].set(str8 / (3600. * 24 * 7))
        self.counters['Years'].set(str8 / (3600. * 24 * 7 * 52))

        self.after(1000, self.increment)

def main():

    root = Tk()
    app = App(root)
    app.pack()
    root.title("STR8")
    root.resizable(width = False, height = False)
    app.mainloop()

if __name__ == '__main__':
    main()

图片:

1 个答案:

答案 0 :(得分:4)

tkinter并非设计为在每个平台上看起来都一样。相反,它专门设计为在每个平台上看起来不同。它被设计为工作相同但不一定相同。只要有可能,它就会使用底层操作系统的小部件工具包来绘制小部件(即:您将在OSX上获得OSX外观按钮,在Windows上获得看起来像窗户的按钮等)。

tkinter的主要目标是在所有平台上运行相同的代码并且工作相同,但尽可能遵守平台准则。