python-3.x - Ttk 框架,背景颜色
问题描述
我正在为我的 GUI 使用 ttk。我知道这也是一个非常简单的问题......我正在尝试更改主窗口的背景颜色。我尝试更改主题,因为我正在使用 Mac(和 Python 3.5)来避免主题“aqua”的问题,这是默认主题。我一直在阅读一些解决方案,例如这些问题同样的问题......这些是问题的数量:
54476511、38712352、47327266、23750141。
但是,我还没有解决这个问题。这是我的代码。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from tkinter.scrolledtext import *
from tkinter import Tk, BOTH, W, N, E, S, messagebox, END
from tkinter.ttk import Button, Label, Style, Frame
class Example(Frame):
def __init__(self,master):
super().__init__()
self.initUI()
def initUI(self):
self.master.title("Example")
Style().theme_use("classic")
self.pack(fill=BOTH, expand=1)
self.columnconfigure(1, weight=1)
self.columnconfigure(3, pad=7)
self.rowconfigure(3, weight=1)
self.rowconfigure(5, pad=7)
self.txt_Pad = ScrolledText(self)
self.txt_Pad.grid(row=1, column=0, columnspan=2, rowspan=4, padx=5, sticky=E+W+S+N)
self.txt_Pad.insert(END,'Type your info here')
btn_save = Button(self, text="Save", command=self.save_command)
btn_save.grid(row=1, column=3)
btn_close = Button(self, text="Close", command=self.onClose)
btn_close.grid(row=2, column=3, pady=4)
btn_help = Button(self, text="Help", command=self.about_command)
btn_help.grid(row=5, column=0, padx=5)
def onClose(self):
self.master.destroy()
def about_command(self):
msb = messagebox.showinfo("About", "\"Insert a useful tip Here\"")
def save_command(self):
print('Your info it\'s save now')
def open_command(self):
print('Choose your File')
def main():
root = Tk()
root.geometry("350x300+300+300")
root.configure(bg='#0059b3')
app = Example(root)
root.mainloop()
if __name__ == '__main__':
main()
任何建议,将不胜感激。
解决方案
创建一个样式然后应用它。
from tkinter.scrolledtext import *
from tkinter import Tk, BOTH, W, N, E, S, messagebox, END
from tkinter.ttk import Button, Label, Style, Frame
class Example(Frame):
def __init__(self, master):
super().__init__()
self.initUI()
def initUI(self):
self.master.title("Example")
# create a new style
self.style = Style()
# configure it to the background you want
self.style.configure('My.TFrame', background='#0059b3')
#Style().theme_use("classic")
# apply it
self.config(style='My.TFrame')
self.pack(fill=BOTH, expand=1)
self.columnconfigure(1, weight=1)
self.columnconfigure(3, pad=7)
self.rowconfigure(3, weight=1)
self.rowconfigure(5, pad=7)
self.txt_Pad = ScrolledText(self)
self.txt_Pad.grid(row=1, column=0, columnspan=2, rowspan=4, padx=5, sticky=E+W+S+N)
self.txt_Pad.insert(END,'Type your info here')
btn_save = Button(self, text="Save", command=self.save_command)
btn_save.grid(row=1, column=3)
btn_close = Button(self, text="Close", command=self.onClose)
btn_close.grid(row=2, column=3, pady=4)
btn_help = Button(self, text="Help", command=self.about_command)
btn_help.grid(row=5, column=0, padx=5)
def onClose(self):
self.master.destroy()
def about_command(self):
msb = messagebox.showinfo("About", "\"Insert a useful tip Here\"")
def save_command(self):
print('Your info it\'s save now')
def open_command(self):
print('Choose your File')
def main():
root = Tk()
root.geometry("350x300+300+300")
root.configure(background='#0059b3')
app = Example(root)
root.mainloop()
if __name__ == '__main__':
main()
我在我改变的部分留下了评论。
推荐阅读
- docker - 如何在 confluent-kafka 中启用代理安全性?
- angular - 如何在 ngx-dropzone-image-preview 上显示上传图片
- excel - 如何在 Microsoft“AddressOf 运算符”文档页面上运行 VBA 示例
- wordpress - 是否有任何可以在帖子中显示革命滑块的视频?
- javascript - 将 javascript 代码转换为图像而不是表情符号
- asp.net - ASP.NET Web 窗体和 React
- javascript - TypeError:“data”参数必须是字符串类型或 Buffer、TypedArray 或 DataView 的实例。收到 IncomingMessage 的一个实例
- windows - 无法获得正确的打印机状态
- java - com.predic8.soamodel.TypeRefAccessException 在为 WSDL 创建 SOAP 请求模板时
- docusignapi - 签名工具不适用于嵌入式签名