如何在Tkinter小部件中更改边框颜色?

时间:2020-02-06 13:00:34

标签: python tkinter border tk border-color

我想知道如何更改tkinter Label或Button的边框颜色,我将releif放在solid中,边框颜色将为black。我已经尝试过highlightthicknesshighlightcolorhighlightbackground,但没有用

这是我的代码的示例:

import tkinter as tk 

root = tk.Tk()
root.geometry("800x450")
root.title("How should i change border color")
tk.Label(root,text = "How should i change border color",width = 50 , height = 4 ,bg = "White",relief = "solid").place(x=10,y=10)
tk.Button(root,text = "Button",width = 5 , height = 1 ,bg = "White",relief = "solid").place(x=100,y=100)


root.mainloop()

这是我要更改的内容(边框颜色现在是Black,我想将其更改为红色):

image

我已经尝试了您所说的@moshe-perez,但是它不起作用: image

3 个答案:

答案 0 :(得分:0)

使用""" recordFile.py records audio from the default microphone in a background thread using pyaudio. """ import pyaudio import wave import threading import time import subprocess from tkinter import messagebox CHUNK = 1024 FORMAT = pyaudio.paInt16 CHANNELS = 2 RATE = 44100 RECORD_SECONDS = 5 # WAVE_OUTPUT_FILENAME = "tmp/tmp.wav" class recorder: def __init__(self): self.going = False # is the process running? self.process = None # stores a reference to the background thread self.filename = "" # the name of the file to record to self.p = pyaudio.PyAudio() self.devices = [None] self.error = False def record(self, filename): # end the process before starting a new one if self.process and self.process.is_alive(): self.going = False self.error = False # start a recording thread self.process = threading.Thread(target=self._record) self.process.start() self.filename = filename def _record(self): try: # initialize pyaudio streams = [] frames = [] # stores audio data for i in range(len(self.devices)): streams.append(self.p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK, input_device_index=self.devices[i])) frames.append([]) print("* recording") self.going = True # let the system know that we are running while self.going: # stream the audio into "frames" for i in range(len(self.devices)): data = streams[i].read(CHUNK) frames[i].append(data) print("* done recording") # stop recording for i in range(len(self.devices)): streams[i].stop_stream() streams[i].close() # write the audio data to a file (tmp/tmp.wav) for i in range(len(self.devices)): wf = wave.open( self.filename[:self.filename.find(".")] + "_" + str(i) + self.filename[self.filename.find("."):], 'wb') wf.setnchannels(CHANNELS) wf.setsampwidth(self.p.get_sample_size(FORMAT)) wf.setframerate(RATE) wf.writeframes(b''.join(frames[i])) wf.close() except Exception as e: self.error = True messagebox.showerror("AUDIO ERROR", "ERROR ENCOUNTERED RECORDING AUDIO: " + str(e)) def getDeviceCount(self): return self.p.get_device_count() def getDeviceName(self, deviceID): return self.p.get_device_info_by_index(deviceID)["name"] def isInputDevice(self, deviceID): return int(self.p.get_device_info_by_index(deviceID)["maxInputChannels"]) > 0 def getAPIName(self, deviceID): return self.p.get_host_api_info_by_index(self.p.get_device_info_by_index(deviceID)["hostApi"])["name"] def setToDefault(self): self.devices = [None] def setToDevices(self, devices): self.devices = devices def stop_recording(self): self.going = False def destroy(self): self.p.terminate() 时,需要提供颜色代码,例如highlightbackground。 因此,请使用"#37d3ff"并删除highlightbackground="COLORCODE"

例如:

relief="solid"

结果:

enter image description here

更新:虽然它可以在我的ubuntu机器上运行,但我只是在Windows上对其进行了检查,而在那儿却不起作用。

答案 1 :(得分:0)

AFAIK,tkinter中无法更改边框颜色。我使用的解决方法之一是在根目录中制作一个稍大的标签,然后将标签放入其中。

import tkinter as tk

root = tk.Tk()
root.geometry("800x450")
root.title("How should i change border color")
border = tk.Label(root, width=52, height=5, bg='red')
border.place(x=10, y=10)
tk.Label(border, text="How should i change border color", width=50, height=4, bg="White", highlightthickness=4, highlightbackground="#37d3ff").place(x=1, y=1)
tk.Button(root, text="Button", width=5, height=1, bg="White", highlightbackground="#37d3ff").place(x=100, y=100)


root.mainloop()

不漂亮,但是可以。 enter image description here

答案 2 :(得分:0)

这可能会有所帮助,因为我使用了Frame

我可以更改背景颜色。

import tkinter as tk 

root = tk.Tk()
root.geometry("800x450")
root.title("How should i change border color")
border = tk.Frame(root, background="green")
label = tk.Label(border, text="How should i change border color", bd=5)
label.pack(fill="both", expand=True, padx=5, pady=5)
border.pack(padx=20, pady=20)
button1 = tk.Button(root, background="green")
name = tk.Button(button1, text="click", bd=0)
name.pack(fill="both", expand=True, padx=2, pady=2)
button1.pack(padx=20, pady=20)


root.mainloop()

很长但是很努力。