ttk 应用样式时打开辅助窗口 [英] ttk Opening Secondary Window when Applying Style

查看:24
本文介绍了ttk 应用样式时打开辅助窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

from Tkinter import *导入 ttk, tkMessageBox导入操作系统字体 = ("Avenir", 24)b = ttk.Style()b.configure('TButton', font=font)类 LoginScreen(Tk):def __init__(self, *args, **kwargs):Tk.__init__(self, *args, **kwargs)容器 = 框架(自我)container.pack(side=TOP,fill=BOTH,expand=True)self.frames = {}对于 F in(登录,注册):框架 = F(容器,自身)self.frames[F] = 框架frame.grid(row=0, column=0, sticky='new')self.show_frame(登录)def show_frame(self, cont):frame = self.frames[续]frame.tkraise()类登录(框架):def __init__(self, parent, controller):Frame.__init__(self, parent)label = Label(self, text="screen 1")button = Button(self, text="move", font=font, command=lambda: controller.show_frame(Register))按钮.pack()标签.pack()类寄存器(帧):def __init__(self, parent, controller):Frame.__init__(self, parent)label = Label(self, text="screen 2")标签.pack()如果 __name__ == '__main__':应用程序 = 登录屏幕()app.title("登录")app.mainloop()

当我运行这个时,我得到这个屏幕:没有ttk的工作屏幕

但是一旦我改变:

button = Button(self, text="move", font=font, command=lambda: controller.show_frame(Register))

button = ttk.Button(self, text="move", style='TButton', command=lambda: controller.show_frame(Register))

第二个窗口打开,字体不变.>

我希望我忽略了一些简单的东西,但是这种设置 ttk 小部件样式的方法是我在网上看到的唯一方法.

我不想要窗口,正如我之前所说,当我将b"样式应用于按钮时,它似乎神奇地出现了.

感谢阅读.

解决方案

次窗口是由你的第 7 行引起的.当你调用 ttk.Style 时,它需要一个根窗口来处理,并且如果一个还没有被创建,它会创建一个.要解决此问题,您需要在创建根窗口后将第 7 行和第 8 行移动到某个点(调用 Tk()).

如果 __name__ == '__main__':应用程序 = 登录屏幕()app.title("登录")b = ttk.Style()b.configure('TButton', font=font)app.mainloop()

Here is my code

from Tkinter import *
import ttk, tkMessageBox
import os

font = ("Avenir", 24)

b = ttk.Style()
b.configure('TButton', font=font)

class LoginScreen(Tk):
    def __init__(self, *args, **kwargs):
        Tk.__init__(self, *args, **kwargs)

        container = Frame(self)
        container.pack(side=TOP, fill=BOTH, expand=True)

        self.frames = {}
        for F in (Login, Register):
            frame = F(container, self)
            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky='nsew')

        self.show_frame(Login)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

class Login(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        label = Label(self, text="screen 1")
        button = Button(self, text="move", font=font, command=lambda: controller.show_frame(Register))

        button.pack()
        label.pack()

class Register(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        label = Label(self, text="screen 2")
        label.pack()

if __name__ == '__main__':
    app = LoginScreen()
    app.title("Login")
    app.mainloop()

When I run this, I get this screen: Working Screen without ttk

But as soon as I change:

button = Button(self, text="move", font=font, command=lambda: controller.show_frame(Register))

to

button = ttk.Button(self, text="move", style='TButton', command=lambda: controller.show_frame(Register))

A Secondary Window is opened and the font doesn't change.

I'm hoping there is something simple I am overlooking, but this method of styling ttk widgets is the only way I've seen it done online.

I don't want the window, and as I have stated before it seems to magically appear when I apply the 'b' style to a button.

Thanks for reading.

解决方案

The secondary window is caused by your line 7. When you call ttk.Style it needs a root window to work with, and if one has not been created yet it creates one. To fix this you need to move lines 7 and 8 to a point after creating the root window (calling Tk()).

if __name__ == '__main__':
    app = LoginScreen()
    app.title("Login")
    b = ttk.Style()
    b.configure('TButton', font=font)
    app.mainloop()

这篇关于ttk 应用样式时打开辅助窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆