树莓派Tkinter GUI设置relief='flat'按钮仍有边框问题求助
Hey there! I’ve run into this exact frustration when moving Tkinter apps from Windows to Raspberry Pi—those stubborn button borders that ignore the relief='flat' setting are such a headache. The issue boils down to Raspberry Pi OS using system-level themes (like the default Pi theme or Adwaita) that often override Tkinter’s relief styling for buttons. Let’s walk through some reliable fixes:
1. Force Remove Border Width
Even if relief='flat' gets ignored by the theme, explicitly setting the button’s border width to 0 usually cuts through the default styling. Update your button definitions to include bd=0 (or borderwidth=0):
bouton_break = Button(f2, image=img_break, relief='flat', bd=0, command=break_) bouton_break.place(bordermode=OUTSIDE, height=134, width=107, x=40 , y=200) bouton_MM = Button(f2, image=img_MM, relief='flat', bd=0, command=maint_page) bouton_MM.place(bordermode=OUTSIDE, height=134, width=107, x=170 , y=200) boutonlogout = Button(f2, image=img_logout, relief='flat', bd=0, command=logout_cmd) boutonlogout.place(bordermode=OUTSIDE, height=134, width=107, x=300 , y=200)
This directly tells Tkinter to render no border, bypassing the theme’s enforced styles.
2. Switch to a Theme That Respects Custom Relief
Raspberry Pi’s default theme doesn’t play nice with relief='flat', but the built-in clam theme is far more flexible with custom widget styling. Add this code right after initializing your root window:
# Load the clam theme (adjust path if your Tk version differs) root.tk.call('source', '/usr/share/tk8.6/themes/clam.tcl') root.configure(theme='clam')
Note: Check your Tk version with tkinter.TkVersion—if it’s not 8.6, update the path to match (e.g., tk8.5 instead of tk8.6). This theme gives you full control over relief settings without system overrides.
3. Verify Your Images Have No Built-In Borders
While you said this works on Windows, it’s quick to double-check: make sure your button images (img_break, img_MM, etc.) don’t have their own embedded borders. If they do, you’ll need to edit the image files themselves to remove those edges.
One of these fixes should get your flat buttons looking exactly as intended on Raspberry Pi!
内容的提问来源于stack exchange,提问作者Aksel2099

