Python Tkinter。不适用于第二个程序窗口的主题

时间:2015-03-08 09:49:32

标签: python ubuntu tkinter themes

我正在尝试用Tkinter制作基本的gui。但默认主题" Motif"不是很漂亮,所以我转到了#34; Clam"。但是在我的程序的第二个窗口中的问题,它仍然使用默认主题" Motif"。

首先让我展示一下我的代码:

from Tkinter import *
from ttk import *


def label(row, column, text):
    L = Label(root, text=text, anchor='w')
    L.grid(row=row,column=column,sticky="nw",pady=2,padx=3)


def button(root, row, column, text, command):
    B = Button(root, text=text, command=command, width=15)
    B.grid(row=row, column=column, sticky="e", pady=4, padx=3)


def entry(row, column, insert="", show=""):
    E = Entry(root, width=32)
    E.insert(0, insert)
    E.config(show=show)
    E.grid(row=row,column=column)
    return E


def show_ldif():

    values_list = []
    givenname = var0.get()
    sn = var1.get()
    country = var2.get()
    location = var3.get()
    skype = var8.get()

    cn = givenname[0].lower() + sn.lower()
    email = cn + "@company.com"   

    # ldif is import format for openLDAP
    ldif_list =[]
    ldif_list.append(("dn: cn=%s,cn=people,ou=company,dc=company,dc=com\n") % cn)
    ldif_list.append('c: %s\n'% country)
    ldif_list.append('cn: %s\n'% cn)
    ldif_list.append(('objectclass: inetOrgPerson\n'
                      'objectclass: posixAccount\n'
                      'objectclass: top\n'
                      'objectclass: shadowAccount\n'
                      'objectclass: ldapPublicKey\n'
                      'objectclass: extensibleObject\n'))

    ldif = ''.join(ldif_list)

    root2 = Tk()
    root2.title("Result")
    root2.style = Style()
    root2.style.theme_use("clam")


    ldif_text = Text(root2, height=30, width=50)
    ldif_text.insert(END, ldif)
    ldif_text.grid(row=0,column=0,columnspan = 2)

    button(root2, 1, 1, "Copy to Clipboard", yes_no)
    button(root2, 1, 0, "Import to LDAP", yes_no)


def yes_no():
    pass


root = Tk()

root.style = Style()
root.style.theme_use("clam")

root.title("LDAP Adder")

label(0, 0, 'First name')
var0 = entry(0, 1)

label(1, 0, 'Second name')
var1 = entry(1, 1)

label(2, 0, 'Country (two letters)')
var2 = entry(2, 1)

label(3, 0, 'City')
var3 = entry(3, 1)

label(8, 0, 'Skype')
var8 = entry(8, 1)

label(13, 0, '')

button(root, 14, 0, 'Show', show_ldif)
button(root, 14, 1, 'Quit', root.quit)

root.mainloop()

第一个窗口看起来很好:

enter image description here

这是Tkinter的平静主题。然后我可以点击按钮' Show'和第二个窗口'结果'将出现:

enter image description here

但是第二个仍然使用默认主题(查看右侧窗口的按钮)。但为什么?我已经激活了#34;平静"第二个窗口的主题:

root2 = Tk()
root2.title("Result")
root2.style = Style()
root2.style.theme_use("clam")

2 个答案:

答案 0 :(得分:2)

这是因为您正在创建Tk的两个实例。这不是tkinter的设计方式。您必须只创建一个Tk实例。如果您需要其他窗口,请创建Toplevel的实例。

答案 1 :(得分:-1)

这样的代码看起来不错:

from Tkinter import *
from ttk import *


def label(row, column, text):
    L = Label(root, text=text, anchor='w')
    L.grid(row=row,column=column,sticky="nw",pady=2,padx=3)


def button(root, row, column, text, command):
    B = Button(root, text=text, command=command, width=15)
    B.grid(row=row, column=column, sticky="e", pady=4, padx=3)


def entry(row, column, insert="", show=""):
    E = Entry(root, width=32)
    E.insert(0, insert)
    E.config(show=show)
    E.grid(row=row,column=column)
    return E


def show_ldif():

    values_list = []
    givenname = var0.get()
    sn = var1.get()
    country = var2.get()
    location = var3.get()
    skype = var8.get()

    cn = givenname[0].lower() + sn.lower()
    email = cn + "@company.com"   

    # ldif is import format for openLDAP
    ldif_list =[]
    ldif_list.append(("dn: cn=%s,cn=people,ou=company,dc=company,dc=com\n") % cn)
    ldif_list.append('c: %s\n'% country)
    ldif_list.append('cn: %s\n'% cn)
    ldif_list.append(('objectclass: inetOrgPerson\n'
                      'objectclass: posixAccount\n'
                      'objectclass: top\n'
                      'objectclass: shadowAccount\n'
                      'objectclass: ldapPublicKey\n'
                      'objectclass: extensibleObject\n'))

    ldif = ''.join(ldif_list)

    top = Toplevel()
    top.title("Result")

    ldif_text = Text(top, height=30, width=50)
    ldif_text.insert(END, ldif)
    ldif_text.grid(row=0,column=0,columnspan = 2)

    button(top, 1, 1, "Copy to Clipboard", yes_no)
    button(top, 1, 0, "Import to LDAP", yes_no)


def yes_no():
    pass


root = Tk()

root.style = Style()
root.style.theme_use("clam")

root.title("LDAP Adder")

label(0, 0, 'First name')
var0 = entry(0, 1)

label(1, 0, 'Second name')
var1 = entry(1, 1)

label(2, 0, 'Country (two letters)')
var2 = entry(2, 1)

label(3, 0, 'City')
var3 = entry(3, 1)

label(8, 0, 'Skype')
var8 = entry(8, 1)

label(13, 0, '')

button(root, 14, 0, 'Show', show_ldif)
button(root, 14, 1, 'Quit', root.quit)

root.mainloop()

所以在我创建新窗口之前:

root2 = Tk()
root2.title("Result")
root2.style = Style()
root2.style.theme_use("clam")

主题无法应用。使用@Rinzler帮助我将此代码更改为:

top = Toplevel()
top.title("Result")

在开始时只激活一次主题。