Python Tkinter。无法为程序的第二个 window 应用主题
Python Tkinter. Cant apply theme for second window of program
我正在尝试使用 Tkinter 制作基本的图形用户界面。但是默认主题 "Motif" 不漂亮所以我切换到 "Clam"。但是我程序的第二个 window 的问题,它仍然使用默认主题 "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()
第一个window看起来不错:
这是 Tkinter 的 Calm 主题。然后我可以点击按钮 'Show',第二个 window 'Result' 会出现:
但是第二个仍然使用默认主题(看右边的按钮window)。但为什么?我已经激活了第二个 "Calm" 主题 window:
root2 = Tk()
root2.title("Result")
root2.style = Style()
root2.style.theme_use("clam")
这样的代码看起来不错:
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()
所以在我创建新的 window 之前:
root2 = Tk()
root2.title("Result")
root2.style = Style()
root2.style.theme_use("clam")
主题无法应用。在@Rinzler 的帮助下,我将此代码更改为:
top = Toplevel()
top.title("Result")
并且一开始只激活了一次主题。
这是因为您正在创建 Tk
的两个实例。这不是 tkinter 的设计用途。您必须恰好创建一个 Tk
实例。如果您需要额外的 windows,请创建 Toplevel
的实例。
我正在尝试使用 Tkinter 制作基本的图形用户界面。但是默认主题 "Motif" 不漂亮所以我切换到 "Clam"。但是我程序的第二个 window 的问题,它仍然使用默认主题 "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()
第一个window看起来不错:
这是 Tkinter 的 Calm 主题。然后我可以点击按钮 'Show',第二个 window 'Result' 会出现:
但是第二个仍然使用默认主题(看右边的按钮window)。但为什么?我已经激活了第二个 "Calm" 主题 window:
root2 = Tk()
root2.title("Result")
root2.style = Style()
root2.style.theme_use("clam")
这样的代码看起来不错:
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()
所以在我创建新的 window 之前:
root2 = Tk()
root2.title("Result")
root2.style = Style()
root2.style.theme_use("clam")
主题无法应用。在@Rinzler 的帮助下,我将此代码更改为:
top = Toplevel()
top.title("Result")
并且一开始只激活了一次主题。
这是因为您正在创建 Tk
的两个实例。这不是 tkinter 的设计用途。您必须恰好创建一个 Tk
实例。如果您需要额外的 windows,请创建 Toplevel
的实例。