Seite 1 von 1

Label verschwindet

Verfasst: Dienstag 3. August 2021, 19:03
von malzbier
Hallo zusammen,
ich lerne grad etwas TKinker und habe ein kleines Problemchen.
Das Fenster zeigt alles richtig an, aber wenn ich das Fenster außerhalb der Bildschirms verschiebe, verschwinden die Labels und werden durch graue
Labels ersetzt und dann steht dort in der Mitte "grey"

Weiß jemand woran das liegt?

VG

Hier ist der Code:

class Volley_field_label(tk.Label):
def __init__(self, parent, **kwargs):
# Defaults
kwargs['text'] = 'grey'
super().__init__(parent, **kwargs)
self.canvas = Canvas(self, width=450, height=400, background='white')
self.coordinates = [60, 150, 210, 50, 150, 210, 50, 150, 210]
self.initUI()


def initUI(self):
'''
self.master.title("Lines")
self.pack(fill=BOTH, expand=1)

canvas = Canvas(self)
canvas.create_line(15, 25, 200, 25)
canvas.create_line(300, 35, 300, 200, dash=(4, 2))
canvas.create_line(55, 85, 155, 85, 105, 180, 55, 85)

canvas.pack(fill=BOTH, expand=1)
'''

self.canvas.grid(row=1,column=0, columnspan=5)
self.canvas.create_line(120, 20, 120, 360, dash=(4, 2))
self.canvas.create_line(180, 20, 180, 360, dash=(4, 2))

self.canvas.create_line(60, 80, 240, 80, dash=(4, 2))
self.canvas.create_line(60, 310, 240, 310, dash=(4, 2))

self.canvas.create_rectangle(60, 20, 240, 360)
self.canvas.create_rectangle(60, 140, 240, 260)
self.canvas.create_line(30, 200, 270, 200 )

self.canvas.create_line(300, 100, 420, 100 )
self.canvas.create_line(340, 70, 340, 130 )
self.canvas.create_line(380, 70, 380, 130 )

self.canvas.create_line(300, 230, 420, 230 )
self.canvas.create_line(340, 200, 340, 260 )
self.canvas.create_line(380, 200, 380, 260 )


def draw_arrow(self, start_position, end_position_field, rating):

start_x = self.get_start_coordinates_x(start_position)
start_y = 210

end_x = self.get_end_coordinates_x(end_position_field)
end_y = self.get_end_coordinates_y(end_position_field)


print(start_x)
print(start_y)
print(end_x)
print(end_y)

self.canvas.create_line(start_x, start_y, end_x, end_y, arrow=tk.LAST)


def get_start_coordinates_x(self, position):
switcher = {
4:70,
3:120,
2:150
}

return switcher.get(position)

def get_end_coordinates_x(self, position):

position = position - 1

self.coordinates[position] = self.coordinates[position] + 7

return self.coordinates[position]

def get_end_coordinates_y(self, position):
switcher = {
1:50,
2:50,
3:50,
4:100,
5:100,
6:100,
7:160,
8:160,
9:160
}

return switcher.get(position)




Main:

from VolleyballField import *
import tkinter as tk
from tkinter import *


root = Tk()

e = Entry(root, width=50)
myLabel2 = Volley_field_label(root)
myLabel3 = Volley_field_label(root)
myLabel4 = Volley_field_label(root)
myLabel3.draw_arrow(4,1, "+")
myLabel3.draw_arrow(4,1, "+")
myLabel3.draw_arrow(4,6, "+")
myLabel3.draw_arrow(4,6, "+")


myLabel2.grid(row=1, column=1)
myLabel3.grid(row=1, column=2)
myLabel4.grid(row=2, column=1)
e.grid(row=2, column=2)

root.mainloop()

Re: Label verschwindet

Verfasst: Dienstag 3. August 2021, 20:40
von __deets__
Ohne Code Tags kann man das nicht lesen.

Re: Label verschwindet

Verfasst: Dienstag 3. August 2021, 21:02
von Dennis89
Hallo,

mit "Zitat einfügen" konnte ich die Einrückungen erkennen. Hier der originale Code des TE:

Code: Alles auswählen

class Volley_field_label(tk.Label):
    def __init__(self, parent, **kwargs):
        # Defaults
        kwargs['text'] = 'grey'
        super().__init__(parent, **kwargs)
        self.canvas = Canvas(self, width=450, height=400, background='white')
        self.coordinates = [60, 150, 210, 50, 150, 210, 50, 150, 210]
        self.initUI()


    def initUI(self):
        '''
        self.master.title("Lines")
        self.pack(fill=BOTH, expand=1)

        canvas = Canvas(self)
        canvas.create_line(15, 25, 200, 25)
        canvas.create_line(300, 35, 300, 200, dash=(4, 2))
        canvas.create_line(55, 85, 155, 85, 105, 180, 55, 85)

        canvas.pack(fill=BOTH, expand=1)
        '''
        
        self.canvas.grid(row=1,column=0, columnspan=5)
        self.canvas.create_line(120, 20, 120, 360, dash=(4, 2))
        self.canvas.create_line(180, 20, 180, 360, dash=(4, 2))
        
        self.canvas.create_line(60, 80, 240, 80, dash=(4, 2))
        self.canvas.create_line(60, 310, 240, 310, dash=(4, 2))
        
        self.canvas.create_rectangle(60, 20, 240, 360)
        self.canvas.create_rectangle(60, 140, 240, 260)
        self.canvas.create_line(30, 200, 270, 200 )
        
        self.canvas.create_line(300, 100, 420, 100 )        
        self.canvas.create_line(340, 70, 340, 130 )
        self.canvas.create_line(380, 70, 380, 130 )

        self.canvas.create_line(300, 230, 420, 230 )        
        self.canvas.create_line(340, 200, 340, 260 )
        self.canvas.create_line(380, 200, 380, 260 )


    def draw_arrow(self, start_position, end_position_field, rating):
            
            start_x = self.get_start_coordinates_x(start_position)
            start_y = 210

            end_x = self.get_end_coordinates_x(end_position_field)
            end_y = self.get_end_coordinates_y(end_position_field)


            print(start_x)
            print(start_y)
            print(end_x)
            print(end_y)

            self.canvas.create_line(start_x, start_y, end_x, end_y, arrow=tk.LAST)
            
    
    def get_start_coordinates_x(self, position):
        switcher = {
            4:70, 
            3:120,
            2:150
        }

        return switcher.get(position)
    
    def get_end_coordinates_x(self, position):
        
        position = position - 1

        self.coordinates[position] = self.coordinates[position] + 7

        return self.coordinates[position]
    
    def get_end_coordinates_y(self, position):
        switcher = {
            1:50, 
            2:50,
            3:50,
            4:100, 
            5:100,
            6:100,
            7:160, 
            8:160,
            9:160
        }

        return switcher.get(position)
Main:

Code: Alles auswählen

from VolleyballField import *
import tkinter as tk
from tkinter import *


root = Tk()

e = Entry(root, width=50)
myLabel2 = Volley_field_label(root)
myLabel3 = Volley_field_label(root)
myLabel4 = Volley_field_label(root)
myLabel3.draw_arrow(4,1, "+")
myLabel3.draw_arrow(4,1, "+")
myLabel3.draw_arrow(4,6, "+")
myLabel3.draw_arrow(4,6, "+")


myLabel2.grid(row=1, column=1)
myLabel3.grid(row=1, column=2)
myLabel4.grid(row=2, column=1)
e.grid(row=2, column=2)

root.mainloop()
Ich habe mal angenommen, dass das zwei Dateien sind wegen des 'Main:'

Grüße
Dennis

Re: Label verschwindet

Verfasst: Mittwoch 4. August 2021, 08:38
von rogerb
@malzbier,

Das Ableiten von tk.Label macht bei dem Volleyball Feld aus meiner Sicht keinen Sinn. Was hat das mit einem Label zu tun? Wenn das Fenster aus dem Bildschirmbereich geschoben wird, siehst du das was du bei dem Label selbst als Text eingesetzt hast "grey". Die Grafik verschwindet dann einfach.

Die einfachste Lösung, bei der dein Problem nicht mehr auftreten sollte, wäre einfach ein Canvas-Element direkt zu verwenden.

Re: Label verschwindet

Verfasst: Mittwoch 4. August 2021, 08:47
von Sirius3
Volleyballfeld darf kein Label sein, warum ist das nicht direkt das Canvas?

Code: Alles auswählen

import tkinter as tk

class VolleyField(tk.Canvas):
    START_COORDINATE = {
        4: (70, 210), 
        3: (120, 210),
        2: (150, 210),
    }
    END_COORDINATES_Y = {
        1:50, 
        2:50,
        3:50,
        4:100, 
        5:100,
        6:100,
        7:160, 
        8:160,
        9:160
    }
    def __init__(self, parent, **kwargs):
        tk.Canvas.__init__(self, parent, width=450, height=400, background='white', **kwargs)
        self.coordinates = [60, 150, 210, 50, 150, 210, 50, 150, 210]
        self.create_line(120, 20, 120, 360, dash=(4, 2))
        self.create_line(180, 20, 180, 360, dash=(4, 2))
        
        self.create_line(60, 80, 240, 80, dash=(4, 2))
        self.create_line(60, 310, 240, 310, dash=(4, 2))
        
        self.create_rectangle(60, 20, 240, 360)
        self.create_rectangle(60, 140, 240, 260)
        self.create_line(30, 200, 270, 200 )
        
        self.create_line(300, 100, 420, 100 )        
        self.create_line(340, 70, 340, 130 )
        self.create_line(380, 70, 380, 130 )

        self.create_line(300, 230, 420, 230 )        
        self.create_line(340, 200, 340, 260 )
        self.create_line(380, 200, 380, 260 )


    def draw_arrow(self, start_position, end_position_field, rating):
        start_x, start_y = self.START_COORDINATE[start_position]
        end_x, end_y = self.get_end_coordinates(end_position_field)
        self.create_line(start_x, start_y, end_x, end_y, arrow=tk.LAST)
            
    def get_end_coordinates(self, position):
        self.coordinates[position - 1] += 7
        return self.coordinates[position - 1], self.END_COORDINATES_Y[position]


def main():
    root = tk.Tk()
    e = tk.Entry(root, width=50)
    e.grid(row=2, column=2)
    volley1 = VolleyField(root)
    volley1.grid(row=1, column=1)
    volley2 = VolleyField(root)
    volley2.grid(row=1, column=2)
    volley3 = VolleyField(root)
    volley3.grid(row=2, column=1)
    volley2.draw_arrow(4,1, "+")
    volley2.draw_arrow(4,1, "+")
    volley2.draw_arrow(4,6, "+")
    volley2.draw_arrow(4,6, "+")
    root.mainloop()

if __name__ == "__main__":
    main()
Man benutzt keine *-Importe und schreibt auch nicht nur eine Klasse in ein Modul, sondern ordnet Funktionen und Klassen thematisch zu Modulen.

Re: Label verschwindet

Verfasst: Mittwoch 4. August 2021, 09:26
von malzbier
Ah ok,
vielen Dank für die Antworten, hat funktioniert!