Tkinter在不同电脑上显示不同

7 投票
2 回答
12844 浏览
提问于 2025-04-18 17:36

我的tkinter窗口在不同的电脑上看起来差别很大(即使它们的分辨率是一样的!):

这是在Windows 8上的样子:

windows 8

这是在Windows 7上的样子:

windows 7

我希望它在第一台电脑上的样子。有什么建议吗?

我的代码是这样的:

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()

2 个回答

3

这可能吗?

虽然这听起来是个很自然的愿望,但由于很多原因,想要实现这个目标在Tk/Tcl中是不可行的,因为它依赖于本地操作系统的窗口管理服务。这里就开始了。{ MacOS、Linux操作系统、Windows操作系统 }的窗口管理器确实做了类似的事情,但它们的外观和感觉并不相同。

最简单的例子就是在第二行中,使用.pack()这个布局管理器时,结果会有很大的不同,尽管那里只有几个简单的Button()按钮。

最接近的解决办法可能是使用“Tk主题小部件”库,也就是ttk

from Tkinter import *
from ttk import *

使用ttk,你可以用ttk.<小部件>来构建图形用户界面,其中有11个已经在Tkinter中存在:ttk.Button、ttk.Checkbutton、ttk.Entry、ttk.Frame、ttk.Label、ttk.LabelFrame、ttk.Menubutton、ttk.PanedWindow、ttk.Radiobutton、ttk.Scale和ttk.Scrollbar,还有一些新的小部件类被添加进来 - Combobox、Notebook、Progressbar、Separator、Sizegrip和Treeview -- 所有这些都很方便地“继承”自小部件。

关键问题是主题

外观和感觉,以及样式代码(加上ttk样式配置的努力)试图帮助实现更好的跨平台用户界面外观。

虽然这让你更接近目标,但在X11终端和Windows 7上实现完全相同的外观和感觉并不容易。

6

是的,使用tkinter制作的应用在不同的平台上看起来会有所不同。这是故意设计的。tkinter会尽量使用操作系统提供的原生控件,也就是那些由操作系统自带的按钮和其他界面元素。这意味着tkinter并不能完全控制图形界面的每一个细节。

在大多数情况下,这种行为是我们希望的。比如,你希望在Windows上看到的按钮看起来像Windows的按钮,而在Mac上看到的按钮则应该像Mac的按钮。这也意味着在Windows 7和Windows 8上,按钮的样子可能会有所不同,因为tkinter只是使用操作系统提供的控件,而微软在每次大版本更新时通常会对这些控件进行调整。

那么,能否在所有平台上得到完全相同的外观呢?这要看情况。你可能在Windows 7和8上通过一些小调整,得到几乎相同的效果。但在Windows和Mac之间,要做到这一点就非常困难了。

不幸的是,这些差异不仅会导致控件的外观不同,还可能造成布局上的差异。例如,不同的平台可能使用不同的字体、默认的边框宽度,以及默认的边距和内边距。这些都会影响窗口的最终布局。

在你的具体情况下,最明显的差别是Windows 7使用的字体比Windows 8稍微小一些。仅这一点就可能对布局产生很大影响。而且,McListBox控件在Windows 7上的边框似乎比在Windows 8上要大。我对这个控件不太了解,所以它可能在不同平台上有不同的默认设置。

大多数这些问题可以通过为边框、内边距、字体等设置明确的数值来解决。有时候需要进行一些实验。

撰写回答