Python 3.x 在类中更改背景Tkinter
我对Tkinter和Python比较陌生,刚开始使用面向对象的Tkinter。 我试图改变所有不同页面的背景颜色,所以我有以下代码Python 3.x 在类中更改背景Tkinter,python-3.x,tkinter,Python 3.x,Tkinter,我对Tkinter和Python比较陌生,刚开始使用面向对象的Tkinter。 我试图改变所有不同页面的背景颜色,所以我有以下代码 import tkinter as tk import sqlite3 from tkinter.ttk import * from tkinter import * LARGE_FONT = ("Verdana", 12) HEIGHT = 700 WIDTH = 800 class programStart(tk.Tk): de
import tkinter as tk
import sqlite3
from tkinter.ttk import *
from tkinter import *
LARGE_FONT = ("Verdana", 12)
HEIGHT = 700
WIDTH = 800
class programStart(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1, minsize=WIDTH)
container.grid_columnconfigure(0, weight=1, minsize=HEIGHT)
self.frames = {}
for F in (StartPage, Register, LoginPage):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise() #Raises to front
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
我尝试过container.configure(bg='red')等等,但没有成功
我如何处理这个问题?试试这个:
将tkinter作为tk导入
导入sqlite3
从tkinter.ttk导入*
从tkinter进口*
大字体=(“Verdana”,12)
高度=700
宽度=800
类programStart(tk.tk):
定义初始化(self,*args,**kwargs):
tk.tk.\uuuuu初始化(self,*args,**kwargs)
容器=tk.框架(自身)
container.pack(side=“top”,fill=“both”,expand=True)
container.grid_rowconfigure(0,重量=1,最小尺寸=宽度)
container.grid_column配置(0,重量=1,最小尺寸=高度)#最小值为0,重量优先
self.frames={}
对于F in(起始页、注册页、登录页):
帧=F(容器,自身,bg=“红色”)
self.frames[F]=帧
frame.grid(行=0,列=0,sticky=“nsew”)
自显示帧(起始页)
def显示画面(自身,续):
帧=自身帧[续]
帧。tkraise()上升到前面
类起始页(传统框架):
定义初始化(自、父、控制器、bg=None、fg=None):
tk.Frame.\uuuuu init\uuuuuu(self,parent,bg=bg=,fg=fg)
#确保所有tkinter小部件都有'bg=bg=,fg=fg'`
基本上,你需要告诉你正在创建的所有小部件背景应该是红色的。创建小部件时,您可以输入
bg
参数(背景)。这是我使用的系统的最低版本,让GUI用户能够根据自己选择的颜色方案更改应用程序中所有小部件的颜色和字体
import tkinter as tk
formats = {
'bg' : 'black',
'fg' : 'white'
}
class Labelx(tk.Label):
def __init__(self, master, *args, **kwargs):
tk.Label.__init__(self, master, *args, **kwargs)
def winfo_subclass(self):
''' a method that works like built-in tkinter method
w.winfo_class() except it gets subclass names
of widget classes custom-made by inheritance '''
subclass = type(self).__name__
return subclass
class Label(Labelx):
'''
If this subclass is detected it will be reconfigured
according to user preferences.
'''
def __init__(self, master, *args, **kwargs):
Labelx.__init__(self, master, *args, **kwargs)
self.config(
bg=formats['bg'],
fg=formats['fg'])
class LabelNegative(Labelx):
'''
If this subclass is detected it will be reconfigured
according to user preferences.
'''
def __init__(self, master, *args, **kwargs):
Labelx.__init__(self, master, *args, **kwargs)
self.config(
bg=formats['fg'],
fg=formats['bg'])
def change_colors():
for widg in (lab1, lab2):
if widg.winfo_class() == 'Label':
if widg.winfo_subclass() == 'Label':
widg.config(bg=formats['fg'], fg=formats['bg'])
elif widg.winfo_subclass() == 'LabelNegative':
widg.config(bg=formats['bg'], fg=formats['fg'])
root = tk.Tk()
f1 = tk.Frame(root)
f1.grid(column=0, row=0)
lab1 = Label(f1, text='Label')
lab1.grid()
lab2 = LabelNegative(f1, text='LabelNegative')
lab2.grid()
button = tk.Button(root, text='colorize', command=change_colors)
button.grid()
root.mainloop()
将frame=F(container,self)
更改为frame=F(container,self,bg=“red”)
,并确保起始页
、寄存器
和登录页
类接受关键字参数。请将初始化方法的代码张贴在起始页
,注册
和登录页
类中,包括“初始化”现在@TheLizzard