Tkinter looks different on different computers

让人想犯罪 __ 提交于 2021-01-26 11:05:30

问题


My tkinter window looks very different on different computers (running on the same resolution!):

windows 8 windows 8

windows 7 windows 7

I want it to look like it does in the first one. Any ideas?

My code looks like this:

class Application():
    def __init__(self):
        self.app = Tk()
        self.app.wm_iconbitmap('.\data\spider.ico')
        self.app.wm_title("Spider Crawler")
        self.app.minsize(width=914, height=331)
        self.app.maxsize(width=914, height=331)

        mainmainleft = Frame(self.app)
        bottom = Frame(self.app)

        mainleft = Frame(mainmainleft)
        itemoptions = Frame(bottom)
        self.graph = multilist.McListBox(bottom)

        startn = Frame(mainleft)
        options = Frame(mainleft)
        hourcredits = Frame(mainleft)

        hoursoption = Frame(hourcredits)
        credits = Frame(hourcredits)

        allbuttons = Frame(mainleft)

        fstart = Frame(startn)

        bp = Frame(allbuttons)
        buttons = Frame(allbuttons)

        self.SchemaUpdate = BooleanVar()
        self.reset = BooleanVar()
        self.genuine = BooleanVar()
        self.buds = BooleanVar()
        self.bills = BooleanVar()
        self.unusual = BooleanVar()
        self.maxs = BooleanVar()
        self.bmoc = BooleanVar()
        self.salvage = BooleanVar()
        self.traded = BooleanVar()
        self.entryid = StringVar()

        self.clicked = False

        for i in [self.reset, self.buds, self.unusual, self.maxs, self.salvage]:
            i.set(True)

        self.b = Button(fstart, text="START", command=self.start)
        c = Button(buttons, text="Steam", command=self.steam).pack(side=LEFT, padx=(0,7))
        d = Button(buttons, text="GotoBP", command=self.backpack).pack(side=LEFT)
        self.b.pack(side=LEFT)

        buttons.pack()
        self.var = StringVar(self.app)
        self.var.set("backpack.tf/profiles/")

        option = OptionMenu(bp, self.var, "backpack.tf/profiles/", "tf2items.com/profiles/", "tf2b.com/tf2/")
        option.config(width = 18)
        option.pack()
        bp.pack(side = BOTTOM)    

        self.box = Entry(startn, textvariable=self.entryid, fg = "gray")
        self.box.bind("<Button-1>", self.callback)
        self.box.insert(0, "Enter steamid")
        self.box.pack(side = TOP, anchor=N, padx =(5,25), pady = 10)

        Label(credits, text="Created by Akenne", font=("Times New Roman", 8)).pack(anchor = E, pady = (0,25))

        credits.pack(side=TOP, anchor=E)

        Label(hoursoption, text="Max Hours:").pack(side=LEFT, padx = (0,10))

        self.hours=IntVar()
        self.hours.set(250)
        Entry(hoursoption,textvariable=self.hours,width=5).pack(side=LEFT)

        hoursoption.pack(padx= (0,45))

        Checkbutton(options, text = "Reload item schema", variable = self.SchemaUpdate).pack(side=TOP, anchor=W, pady =(0, 3))
        Checkbutton(options, text = "Start with fresh id", variable = self.reset).pack(side=TOP, anchor=W)
        Checkbutton(itemoptions, text = "Only never traded items", variable = self.traded).pack(side=TOP, anchor=W)
        Checkbutton(itemoptions, text = "Genuines", variable = self.genuine).pack(side=TOP, anchor=W)
        Checkbutton(itemoptions, text = "Earbuds", variable = self.buds).pack(side=TOP, anchor=W)
        Checkbutton(itemoptions, text = "Bill's", variable = self.bills).pack(side=TOP, anchor=W)
        Checkbutton(itemoptions, text = "Unusuals", variable = self.unusual).pack(side=TOP, anchor=W)
        Checkbutton(itemoptions, text = "Max's items", variable = self.maxs).pack(side=TOP, anchor=W)
        Checkbutton(itemoptions, text = "BMOCs", variable = self.bmoc).pack(side=TOP, anchor=W)
        Checkbutton(itemoptions, text = "Salvaged crates", variable = self.salvage).pack(side=TOP, anchor=W)
        self.lbl = Label(fstart, text="0/0 found")
        self.lbl.pack(side = LEFT, anchor = W, padx = (20,30))

        fstart.pack(side=TOP)

        startn.pack(side=LEFT, anchor = W, padx = (10, 0))
        options.pack(side=LEFT, padx=(0,30), pady = (5,0))
        allbuttons.pack(side=LEFT, pady=(10,0), padx = (40,0))
        hourcredits.pack(side=LEFT, padx = (95,0), anchor = E)

        mainleft.pack(side = TOP, anchor = W)
        self.graph.container.pack(side = LEFT, anchor = W, pady = 10)
        itemoptions.pack(side=LEFT, anchor = E, padx=7)

        mainmainleft.pack(side = TOP, fill = X)
        bottom.pack(padx =10)
        self.app.mainloop()

回答1:


Yes, tkinter applications will look different on different platforms. This is by design. When possible, Tkinter will use native widgets -- widgets provided by the OS. This means that Tkinter does not have ultimate control over every aspect of the GUI.

Under most circumstances this is the desired behavior. You want a button on Windows to look like a Windows button, and you want a button on OSX to look like an OSX button. This also means that you may get different buttons on Windows 7 vs Windows 8 -- tkinter is just using whatever the OS gives it, and Microsoft tends to tweak its widgets with every major revision.

Can you get the exact same look on all platforms? That depends. You can probably get nearly identical results on Windows 7 and 8 with a small amount of work. It will be very difficult to get the same look between Windows and Macintosh.

Unfortunately, these differences not only result in different looking widgets, but it can cause layout differences as well. For example, different platforms may have different fonts, different default border widths, and default margins and padding. All of this contributes to the final layout of a window.

In your specific case, the most obvious difference is that Windows 7 is using a slightly smaller font than Windows 8. That alone can have a big effect on layout. It also appears that the McListBox widget has a larger border around it on windows 7 than on windows 8. I don't know anything about that widget, so it could have different defaults based on the platform.

Most of these problems can be resolved by using explicit values for borders, padding, fonts, etc. Sometimes it takes a little experimentation.




回答2:


Is it possible?

While it seems as a natural wish, the target is not possible due to many reasons in Tk/Tcl reliance on localhost O/S windowing manager services. Here it starts. { MacOS, Linux OS, W* OS }-WM-s definitely do similar tasks, however, not with a same look-and-feel.

Simplest seen on rather different results of the .pack() Geometry Manager outputs on row two with just a few trivial Button()-s there

The closest hit may be to use "Tk Themed Widget" library, a.k.a. ttk

from Tkinter import *
from ttk import *

With ttk you construct GUI-s with ttk.<Widgets>, 11 of which already existed in Tkinter: ttk.Button, ttk.Checkbutton, ttk.Entry, ttk.Frame, ttk.Label, ttk.LabelFrame, ttk.Menubutton, ttk.PanedWindow, ttk.Radiobutton, ttk.Scale and ttk.Scrollbar plus some new widget classes are added - Combobox, Notebook, Progressbar, Separator, Sizegrip and Treeview -- all of which comfortly "subclass" from Widget.

Key issue is the theme-ing

The look-and-feel as well as the styling code ( + ttk style-configuration efforts ) tries to help achieve a better cross-platform UI appearance.

While this brings you a way closer, it is not easy to have identical look-an-feel both on X11-terminal and W7.



来源:https://stackoverflow.com/questions/25354160/tkinter-looks-different-on-different-computers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!