Jan-15-2024, 06:00 AM
Hi All - How can I remove the button frame in my code? I have tried a few examples I found, but have had no luck. I see one argument example for 'borderwidth=0' but does not seem to change anything. I am using a toggle button with images. Attached picture is what I have showing for my button. The image has rounded corners, so I want to remove the frame of the button completely. Thanks for any help with this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
import tkinter as tk from tkinter import * from tkinter import messagebox import requests import http.client import time import json path = "" sonoff_url = 'NOT_INIT' class RelayButton(tk.Button): """A button that toggles a relay""" def __init__( self , parent, url = "", on_image = None , off_image = None , * * kwargs): super ().__init__(parent, image = off_image, command = self .toggle) self .url = url self ._on = False self .on_image = on_image self .off_image = off_image self .label = Label(text = " ", bg=" White ", fg=" Black ", font=(" Helvetica", 12 )) self .label.place(x = 10 ,y = 15 ) self .update_clock() self .templabel = Label(text = "temp info" , bg = "White" , fg = "Black" , font = ( "Helvetica" , 12 )) self .templabel.place(x = 10 , y = 40 ) #self.getTemp() self .master.configure(background = "white" ) @property def on( self ): """Return state of the relay""" return self ._on @on .setter def on( self , on): """Set state of the relay""" self ._on = on try : if on: requests.post( f "{self.url}cm?cmnd=Power On" ) self [ "image" ] = self .on_image else : requests.post( f "{self.url}cm?cmnd=Power Off" ) self [ "image" ] = self .off_image except : messagebox.showinfo( "Error" ) def toggle( self ): """Toggle state of the relay""" self .on = not self .on def update_clock( self ): now = time.strftime( "%I:%M:%S %p " + ' - ' + "%x" ) self .label.configure(text = 'Time/Date: ' + now) self .after( 1000 , self .update_clock) def getTemp( self ): sonoff_url = SONOFF_Temp sonR1 = requests.get(sonoff_url) x = json.loads(sonR1.text) status = x[ "StatusSNS" ] timestamp = status[ "Time" ] device_names = list (status.keys())[ 1 : - 1 ] temp_units = status[ "TempUnit" ] for name in device_names: device = status[name] #print(f'ID={name}, Temperature={device["Temperature"]} {temp_units}') self .templabel.configure(text = 'Living Room ' + f 'Temperature: {device["Temperature"]} {temp_units}' ) self .after( 5000 , self .getTemp) def new_button(name, url, parent, row, column, * * kwargs): """Convenience functon that creates a RelayButton""" on_image = tk.PhotoImage( file = f "{path}{name}_on.png" ) off_image = tk.PhotoImage( file = f "{path}{name}_off.png" ) button = RelayButton(parent, url, on_image, off_image, * * kwargs) button.grid(row = row, column = column, padx = 70 , pady = 110 ) return button def main(): app = tk.Tk() app.title( 'Home Relay Control' ) buttons = [ #new_button('lvgRoom', "http://192.168.0.101/", app, 0, 0), ] for b in buttons: b.on = False app.mainloop() if __name__ = = "__main__" : main() |