简体   繁体   English

如何使用 tkinter 更改我的背景颜色?

[英]How to change my background color with tkinter?

Basically i'm trying to change the background color of this code but it doesn't do anything (it doesn't throw an error but the background color does not change), I've tried a lot of different things but it doesn't do anything, what do i need to do or what is the problem here?基本上我正在尝试更改此代码的背景颜色,但它没有做任何事情(它不会引发错误,但背景颜色不会改变),我尝试了很多不同的东西,但它没有不做任何事情,我需要做什么或这里有什么问题?

I have try several different commands but nothing seems to work.我尝试了几个不同的命令,但似乎没有任何效果。

The funny thing is that I did a little test code to see if this was a typo or something, and in my test code the background color DOES change, i don't know why it doesn't work in the main code, TEST CODE:有趣的是我做了一些测试代码来看看这是不是一个错字还是什么,在我的测试代码中背景颜色确实改变了,我不知道为什么它在主代码中不起作用,测试代码:

    import tkinter

    root = tkinter.Tk()

    frame = tkinter.Frame(root)
    frame.grid(column=0, row=0)

    tkinter.Button(frame,text="Open file",command=None).grid(column=0, row=1 )
    lab = tkinter.Label(frame, text="test test test test test test ")
    lab.grid(column=0, row=2)

    root.configure(background='black')
    lab.configure(background='black', foreground='white')
    frame.configure(background='black')

    root.mainloop()

MAIN CODE:主要代码:

import tkinter as tk
from PIL import ImageTk, Image
import os
import requests
from io import BytesIO

root = tk.Tk()
root.title('PRUEBAAAAAASASASASA')
root.configure(bg='#00ff00')
img_url = "http://atlanticschools.net/wp-content/uploads/2017/05/PISA_LOGO-04.png"
response = requests.get(img_url)
img_data = response.content
img = ImageTk.PhotoImage(Image.open(BytesIO(img_data)))
panel = tk.Label(root, image=img)

panel.pack(side="bottom", fill="both", expand="yes")

root.configure(background='black')
root.mainloop()

The output from the main code is just the image and a default background and the output from the test code does have a changed background color主代码中的 output 只是图像和默认背景,而测试代码中的 output 确实具有更改的背景颜色

Your panel label is taking all the space of the root window.您的panel label 占用了根 window 的所有空间。 So to change the bg color, config its background colour instead.因此,要更改背景颜色,请改为配置其背景颜色。

panel = tk.Label(root, image=img, bg="black")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM