Wie ändere ich von einem Button oder Label den border-style?

Programmierung für GNOME und GTK+, GUI-Erstellung mit Glade.
Antworten
Alfons Mittelmeyer
User
Beiträge: 1715
Registriert: Freitag 31. Juli 2015, 13:34

Donnerstag 31. August 2017, 19:52

Und damit meine ich nicht für alle Buttons oder Labels, sondern nur für einen.

Ich finde Doku über CSS und Gtk+ aber wie geht das in Python pyObject?
Alfons Mittelmeyer
User
Beiträge: 1715
Registriert: Freitag 31. Juli 2015, 13:34

Donnerstag 31. August 2017, 23:33

Habe es herausgefunden, aber auf meinem Pi3 gehen ridge und groove nicht.

Bild

Hat jemand auch einen Raspberry und es funktiert da?

Das ist das Programm:

Code: Alles auswählen

import TkGtk as tk

class Application(tk.Tk):
    def __init__(self,**kwargs):
        tk.Tk.__init__(self,**kwargs)

        self.title('Hello World')

        self.title('Hello World')

        widget = tk.Label(self,text="none",relief='flat',bd=5, bg = '#d9d9d9', fg = 'black')
        widget.grid(row=0,column=0)

        widget = tk.Label(self,text="solid",relief='solid',bd=5, bg = '#d9d9d9', bordercolor = 'black', fg = 'black')
        widget.grid(row=0,column=1)

        widget = tk.Label(self,text="ridge",relief='ridge',bd=5, bg = '#d9d9d9', bordercolor = '#a8a8a8', fg = 'black')
        widget.grid(row=0,column=2)

        widget = tk.Label(self,text="outset",relief='raised',bd=5, bg = '#d9d9d9', bordercolor = '#b0b0b0', fg = 'black')
        widget.grid(row=1,column=0)

        widget = tk.Label(self,text="inset",relief='sunken',bd=5, bg = '#d9d9d9', bordercolor = '#b0b0b0', fg = 'black')
        widget.grid(row=1,column=1)

        widget = tk.Label(self,text="groove",relief='groove',bd=5, bg = '#d9d9d9', bordercolor = '#b0b0b0', fg = 'black')
        widget.grid(row=1,column=2)

Application().mainloop()
Und damit es läuft, braucht man noch TkGtk.py

Code: Alles auswählen

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk,Gdk

def style_string(tkgtk_style):
    st_str = ''
    for key in sorted(tkgtk_style.keys()):
        st_str += key + ' {'
        for in_key, value in tkgtk_style[key].items():
          st_str += '{} : {};'.format(in_key,value)  
        st_str +=  ' }\n'
    return bytes(st_str,'utf-8')

class TkGtkGuiElement:

    def __init__(self,master,**kwargs):
        self.master = master
        self.tkgtk_style = { self.tkgtk_type : { } }


        self.config(**kwargs)

    def __setitem__(self,key,value):
        conf = { key : value }
        self.config(**conf)

    def config(self,**kwargs):
        text = kwargs.pop('text',None)
        if text != None: self.set_text(text)

        bg = kwargs.pop('bg',None)
        if bg == None: bg = kwargs.pop('background',None)
            
        fg = kwargs.pop('fg',None)
        if fg == None: fg = kwargs.pop('foreground',None)
        
        relief = kwargs.pop('relief',None)
        if relief == 'flat': relief = 'none'
        elif relief == 'sunken' : relief = 'inset'
        elif relief == 'raised' : relief = 'outset'


        bd = kwargs.pop('bd',None)
        if bd == None: bd = kwargs.pop('borderwidth',None)
            
        bordercolor = kwargs.pop('bordercolor',None)

        activeforeground = kwargs.pop('activeforeground',None)

        set_style = False
        if bg != None:
            self.tkgtk_style[self.tkgtk_type]['background-color'] = bg
            set_style = True

        activebackground = kwargs.pop('activebackground',None)

        if activebackground != None:
            entry = self.tkgtk_type+':hover'
            if entry not in self.tkgtk_style:
                self.tkgtk_style[entry] = {}
            self.tkgtk_style[entry]['background-color'] = activebackground
            set_style = True

        if activeforeground != None:
            entry = self.tkgtk_type+':hover'
            if entry not in self.tkgtk_style:
                self.tkgtk_style[entry] = {}
            self.tkgtk_style[entry]['color'] = activeforeground
            set_style = True

        if fg != None:
            self.tkgtk_style[self.tkgtk_type]['color'] = fg
            set_style = True

        if relief != None:
            self.tkgtk_style[self.tkgtk_type]['border-style'] = relief
            set_style = True

        if bd != None:
            self.tkgtk_style[self.tkgtk_type]['border-width'] = str(bd)+'px'
            set_style = True

        if bordercolor != None:
            self.tkgtk_style[self.tkgtk_type]['border-color'] = bordercolor
            set_style = True

        if kwargs:
            for key,value in kwargs.items():
                self.tkgtk_style[self.tkgtk_type][key] = value
            set_style = True

        if set_style:

            css = Gtk.CssProvider()
            css.load_from_data(style_string(self.tkgtk_style))
            style_context = self.get_style_context()
            style_context.add_provider(css,Gtk.STYLE_PROVIDER_PRIORITY_SETTINGS)

    def grid(self,row=0,column=0,rowspan=1,columnspan=1,sticky=''):
        self.master.layout.attach(self,column,row,columnspan,rowspan)


class TkGtkGuiContainer(TkGtkGuiElement):

    def __init__(self,master,**kwargs):
        TkGtkGuiElement.__init__(self,master,**kwargs)
        self.layout = Gtk.Grid()
        master.add(self.layout)


class Label(Gtk.Label,TkGtkGuiElement):

    def __init__(self,master,**kwargs):
        Gtk.Label.__init__(self)
        self.tkgtk_type = 'GtkLabel'
        TkGtkGuiElement.__init__(self,master,**kwargs)


class Tk(Gtk.Window,TkGtkGuiContainer):

    def __init__(self,**kwargs):
        Gtk.Window.__init__(self,title="TkGtk")
        self.tkgtk_type = 'GtkWindow'
        TkGtkGuiContainer.__init__(self,self,**kwargs)

        self.connect("delete-event", Gtk.main_quit)

    def title(self,text):
        self.set_title(text)

    def mainloop(self):
        self.show_all()
        Gtk.main()
Antworten