从不同文件导入窗口小部件样式(类似于css)

时间:2016-04-11 13:32:21

标签: python tkinter tkinter-canvas

我正在尝试将pyfont.py文件中编写的样式用于mainfile.py。 请帮助我如何在mainfile.py中访问p.Tbutton 代码:

mainfile.py
from tkinter import *
from tkinter import ttk


import tkinter as tk

class Login(Frame):


          def __init__(self, master):

                    Frame.__init__(self, master)
                    self.grid()
                    self.create_widgets()
                    self.place(relx=.5, rely=.5, anchor="c")

          def create_widgets(self):
                    self.userLbl = Label(self, text = "UserName")
                    self.userLbl.grid()
                    self.usernameEntry = Entry(self)
                    self.usernameEntry.grid(row = 0, column = 1 ,sticky = W, padx=(50,50), pady=(20,20))
                    self.passwrdLbl = Label(self, text = "Password")
                    self.passwrdLbl.grid(row = 1, column = 0 ,sticky = W, padx=(50,50), pady=(20,20))
                    self.passEntry = Entry(self, show="*")
                    self.passEntry.grid(row = 1, column = 1 ,sticky = W, padx=(50,50), pady=(20,20))
                    self.submit_bttn = ttk.Button(self, text = "Login",  style="p.TButton")
                    self.submit_bttn.grid(row = 2, column = 1 ,sticky = W, padx=(50,50), pady=(50,50))


# main
root = Tk()
root.title("Login")
root.geometry("800x400")
app = Login(root)
root.mainloop()


pyfont.py file

import tkinter
from tkinter import ttk

style1 = tkinter.ttk.Style()
style1.configure("BW.TLabel", foreground="black", background="white")

style1.configure("BW.TButton", foreground="red", background="white")

style1.configure("p.TButton", foreground="yellow", background="white")

此代码处于运行状态,但样式未反映在botton上。任何帮助都将受到高度赞赏。

0 个答案:

没有答案