如何更改tkinter backround应用程序的特定部分?

2024-06-16 10:50:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我想更改我的应用程序的背景颜色,当前的方式如下: enter image description here

我希望我的应用程序看起来也是这样的: enter image description here

你可以看到它有一些不同的颜色,比如在顶部,整个区域看起来有点灰色,我想看看怎么做, 这是我的代码:

root.title("Arizon")
root.geometry("620x400+0+0")
root.configure(bg='#1c1b1c')
heading = Label(root, text="Arizon Updater", font=("arial", 40, "bold"), fg="#030208", bg= "#1c1b1c").pack()
label1 = Label(root, text="Enter how much minimum value do u want to gain: ", font=("arial", 9, "bold"), fg="#f0f0f5", bg="#141314").place(x=5, y=90)

Tags: text应用程序区域颜色方式rootlabelbg
1条回答
网友
1楼 · 发布于 2024-06-16 10:50:13

在评论中解释

import tkinter as tk


class App(tk.Tk):
    #width, height and title are constants of your app so, write them as such
    WIDTH  = 620
    HEIGHT = 400
    TITLE  = 'Arizon'

    def __init__(self):
        tk.Tk.__init__(self)
        #set the app bg color
        self.configure(bg="gray2")

        #set the header bg color to something different than the app bg color
        header = tk.Label(self, text="Arizon Updater", font="arial 40 bold", fg="gray70", bg="gray18")

        #tell the header to be at the top and to fill it left to right
        header.pack(anchor='nw', fill="x")

        #name things what they are, this is a label, but more importantly it is your first question
        question_1 = tk.Label(self, text="Enter the minimum amount you want to gain: ", font="arial 9 bold", fg="gray70", bg='gray2')
        question_1.place(x=5, y=90)


#use proper PEP8 to initialize your program
if __name__ == "__main__":
    app = App()
    app.geometry(f'{App.WIDTH}x{App.HEIGHT}')
    app.title(App.TITLE)
    app.mainloop()

旁白:我修正了你第一个问题的语法


注意:

我写的一些东西对你来说可能是不必要的,直到你意识到你所做的每一个应用都可以从下面的模板开始。只需更改WIDTHHEIGHTTITLE值即可为您想要构建的内容设置舞台。它不是“最好”的模板,但肯定不是一个坏模板。当你变得更好时,你可以使你的模板更好,并理解为什么它更好

import tkinter as tk

class App(tk.Tk):
    WIDTH  = 620
    HEIGHT = 400
    TITLE  = 'Arizon'

    def __init__(self):
        tk.Tk.__init__(self)


if __name__ == "__main__":
    app = App()
    app.geometry(f'{App.WIDTH}x{App.HEIGHT}')
    app.title(App.TITLE)
    app.mainloop()

我强烈建议你花点时间阅读文档。如果你不这样做,你会不断地挣扎和失败。即使你的成功也只不过是一个巨大的复制/粘贴混乱。文档是你应该建立你所有知识的基础。我已经编程25年了,精通20多种语言。我没有任何问题。我完全知道我在说什么。研究这些文件就像他们是圣经一样

“Tk允许有Frames。有Frames,它是self.good~文件1.013rc的书

:D

相关问题 更多 >