Seite 2 von 2

Re: button placement.

Verfasst: Montag 29. Januar 2018, 17:45
von jan.b
Damit war das positionieren eines buttons nach ausgewählten Koordinaten innerhalb eines canvas Objektes gemeint.

Re: button placement.

Verfasst: Montag 29. Januar 2018, 20:54
von wuf
jan.b hat geschrieben:Damit war das positionieren eines buttons nach ausgewählten Koordinaten innerhalb eines canvas Objektes gemeint.
OK! Hier die Variante mit der Platzierung auf einer Canvas:

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tkinter as tk

APP_TITLE = "Button placing on canvas"
APP_XPOS = 100
APP_YPOS = 100
APP_WIDTH = 600
APP_HEIGHT = 200

ANCHORS = ['center', 'n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw']
FONT = ('Helevetica', 14, 'bold')

        
class Application(tk.Tk):

    def __init__(self):
        tk.Tk.__init__(self)
        self.title(APP_TITLE)
        
    def build(self):
        self.protocol("WM_DELETE_WINDOW", self.close_app)
        self.geometry("+{}+{}".format(APP_XPOS, APP_YPOS))
        self.geometry("{}x{}".format(APP_WIDTH, APP_HEIGHT))
        self.option_add("*Button.highlightThickness", 0)
        
        self.canvas = tk.Canvas(self, bg='#FFFF00')
        self.canvas.pack(fill='both', expand=True)
        self.canvas.update_idletasks()
        
        self.pos_var = tk.StringVar()
        self.button = tk.Button(None, textvariable=self.pos_var, width=18,
            height=2, font=FONT)
        self.button.pack(padx=10, pady=10)
        
        self.button_width = self.button.winfo_reqwidth()
        self.button_height = self.button.winfo_reqheight()
        
        self.button_xpos = 0
        self.button_ypos = 0
        self.button_window = self.canvas.create_window(
            self.button_xpos,
            self.button_ypos,
            window=self.button, anchor='nw', tag='button')
        
        self.place_the_button()
        
    def place_the_button(self, pos_index=0):
        canvas_width = self.canvas.winfo_width()
        canvas_height = self.canvas.winfo_height()
        print(self.button_width, self.button_height, canvas_width, canvas_height)
        anchor = ANCHORS[pos_index]
        if anchor == 'center':
            xpos = (canvas_width-self.button_width) / 2
            ypos = (canvas_height-self.button_height) / 2
        elif anchor == 'n':
            xpos = (canvas_width-self.button_width) /2
            ypos = 0
        elif anchor == 'ne':
            xpos = (canvas_width-self.button_width)
            ypos = 0
        elif anchor == 'e':
            xpos = (canvas_width-self.button_width)
            ypos = (canvas_height-self.button_height) / 2
        elif anchor == 'se':
            xpos = (canvas_width-self.button_width)
            ypos = (canvas_height-self.button_height)
        elif anchor == 's':
            xpos = (canvas_width-self.button_width) / 2
            ypos = (canvas_height-self.button_height)
        elif anchor == 'sw':
            xpos = 0
            ypos = (canvas_height-self.button_height)
        elif anchor == 'w':
            xpos = 0
            ypos = (canvas_height-self.button_height) / 2
        elif anchor == 'nw':
            xpos = 0
            ypos = 0
            
        self.pos_var.set("Position:\n'{}'".format(anchor))    
        self.canvas.coords('button', xpos, ypos)
        pos_index += 1
        if pos_index == len(ANCHORS):
            pos_index = 0
            
        self.after(2000,self.place_the_button, pos_index)
        
    def close_app(self):
        # Here do something before apps shutdown
        print("Good Bye!")
        self.destroy()


def main():        
    app = Application()
    app.build()
    app.mainloop()

if __name__ == '__main__':
    main()
Gruss wuf :wink:

Re: button placement.

Verfasst: Montag 29. Januar 2018, 22:07
von jan.b
100 zeilen? ganz schön viel. danke für deine ausführliche hilfe. bis ich das ganz verstanden habe, muss ich wohl noch ein bisschen rum googeln, aber wenigstens die Basis habe ich jetzt. :D

Re: button placement.

Verfasst: Dienstag 30. Januar 2018, 07:49
von wuf
Hi jan.b

Hier noch eine Variante mit wesentlich weniger Zeilen:

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tkinter as tk
 
       
class Application(tk.Tk):

    def __init__(self, title):
        tk.Tk.__init__(self)
        self.title(title)
        
        self.geometry("300x300")
        
        self.canvas = tk.Canvas(self, bg='#FFFF00')
        self.canvas.pack(fill='both', expand=True)
        self.canvas.update_idletasks()
        
        self.pos_var = tk.StringVar()
        self.button = tk.Button(None, text="Button-Widget", width=10)
        self.button.pack()
        
        # Alte Button Position        
        xpos = 0
        ypos = 0
        self.button_window = self.canvas.create_window(xpos, ypos,
            window=self.button, anchor='nw', tag='button')

        # Neue Button Position
        xpos = 150
        ypos = 150
        self.canvas.coords('button', xpos, ypos)


Application("Button placing on canvas").mainloop()
Gruss wuf :wink:

Re: button placement.

Verfasst: Dienstag 30. Januar 2018, 19:03
von jan.b
danke. ehrlich gesagt ist das sehr viel hilfreicher und motiviert mich dazu das ganze auch richtig durchzugehen. :D