2017-09-09 1 views
1

Hey les gars im essayant de montrer le texte dans une infobulle pour ressembler à une table en python, j'ai essayé d'utiliser | et - mais ça ne semble pas très bien, vous pouvez voir sur la photo comment ça s'est passé. enter image description hereFaire apparaître le texte de l'info-bulle comme une table?

class CreateToolTip(object): 
''' 
create a tooltip for a given widget 
''' 
    def __init__(self, widget, text='widget info'): 
     self.widget = widget 
     self.text = text 
     self.widget.bind("<Enter>", self.enter) 
     self.widget.bind("<Leave>", self.close) 
    def enter(self, event=None): 
     x = y = 0 
     x, y, cx, cy = self.widget.bbox("insert") 
     x += self.widget.winfo_rootx() + 25 
     y += self.widget.winfo_rooty() + 20 
     # creates a toplevel window 
     self.tw = tk.Toplevel(self.widget) 
     # Leaves only the label and removes the app window 
     self.tw.wm_overrideredirect(True) 
     self.tw.wm_geometry("+%d+%d" % (x, y)) 
     label = tk.Label(self.tw, text=self.text, justify='left', 
        background='yellow', relief='solid', borderwidth=1, 
        font=("times", "8", "normal")) 
     label.pack(ipadx=1) 
    def close(self, event=None): 
     if self.tw: 
      self.tw.destroy() 

est ici le code à l'info-bulle im en utilisant

+0

Votre empreinte est cassée. S'il vous plaît pouvez-vous résoudre ce problème. –

+0

Il est maintenant corrigé. – Zitrone

Répondre

1

S'il vous plaît voir mon exemple de code expliqué ci-dessous. Espérons que cela permet d'obtenir ce que vous cherchez:

from tkinter import * 

class App: 
    def __init__(self, root): 
     self.root = root 
     self.labels = [] #array to store labels that we hover over 
     for i in range(3): 
      self.labels.append(Label(self.root, text= "Text"+str(i))) #creates labels that we hover over 
      self.labels[i].pack(fill="both", expand=True) #packs labels that we hover over, needs to be fill="both" and expand=True for some maths later 
      self.labels[i].bind("<Enter>", lambda *args, c=i:self.enter(c)) #bind on enter event 
      self.labels[i].bind("<Leave>", lambda *args, c=i:self.leave(c)) #bind on leave event 
      #for both of the above callbacks we pass in the number which corresponds to the element in the array 
    def enter(self, var): 
     self.var = var 
     self.top = Toplevel(self.root) 
     self.top.wm_overrideredirect(1) #hides the window border and title bar (among other things) 
     self.top.geometry("+%d+%d" % (self.root.winfo_rootx()+self.labels[self.var].winfo_width(), self.root.winfo_rooty()+self.labels[self.var].winfo_y())) 
     #the above line is for placing the toplevel window in the right place 
     #we user self.root.winfo_rootx() and self.root.winfo_rooty() to get the position of the root window on our screen 
     #we then use winfo_width to get the width of the widget we hovered over, and shift the toplevel window to the end of the widget 
     #if you are not using fill="both" and expand=True then you will need to multiply the number by a hard coded value to get it to look "proper" 
     #we also use winfo_y to get the position of the widget we hovered over relative to the window and move the toplevel window down in to positon 
     for i in range(3): #we use these loops to draw the table in the tooltip 
      for c in range(3): 
       Label(self.top, text="Row"+str(i)+"Col"+str(c), borderwidth=1, relief="solid").grid(row=i, column=c) 
    def leave(self, var): 
     self.var = var 
     self.top.destroy() 

root = Tk() 
App(root) 
root.mainloop() 

Le ci-dessous permettra d'atteindre la mise en page de la grille que vous voulez à la place:

from tkinter import * 

class App: 
    def __init__(self, root): 
     self.root = root 
     self.labels = [] #array to store labels that we hover over 
     for i in range(3): 
      self.labels.append(Label(self.root, text= "Text"+str(i))) #creates labels that we hover over 
      self.labels[i].pack(fill="both", expand=True) #packs labels that we hover over, needs to be fill="both" and expand=True for some maths later 
      self.labels[i].bind("<Enter>", lambda *args, c=i:self.enter(c)) #bind on enter event 
      self.labels[i].bind("<Leave>", lambda *args, c=i:self.leave(c)) #bind on leave event 
      #for both of the above callbacks we pass in the number which corresponds to the element in the array 
    def enter(self, var): 
     self.var = var 
     self.top = Toplevel(self.root) 
     self.top.wm_overrideredirect(1) #hides the window border and title bar (among other things) 
     self.top.geometry("+%d+%d" % (self.root.winfo_rootx()+self.labels[self.var].winfo_width(), self.root.winfo_rooty()+self.labels[self.var].winfo_y())) 
     #the above line is for placing the toplevel window in the right place 
     #we user self.root.winfo_rootx() and self.root.winfo_rooty() to get the position of the root window on our screen 
     #we then use winfo_width to get the width of the widget we hovered over, and shift the toplevel window to the end of the widget 
     #if you are not using fill="both" and expand=True then you will need to multiply the number by a hard coded value to get it to look "proper" 
     #we also use winfo_y to get the position of the widget we hovered over relative to the window and move the toplevel window down in to positon 
     for i in range(4): 
      Grid.rowconfigure(self.top, i, weight=1) 
      for c in range(2): 
       Grid.columnconfigure(self.top, c, weight=1) 
       if i != 3: 
        if c == 0: 
         Label(self.top, text="Heading "+str(i), borderwidth=1, relief="solid").grid(column=i, row=c, sticky=N+S+E+W) 
        else: 
         Label(self.top, text="Col "+str(i)+", Row "+str(c), borderwidth=1, relief="solid").grid(column=i, row=c, sticky=N+S+E+W) 
       else: 
        if c == 0: 
         Label(self.top, text="This is some information and stuff which is rowspanning", borderwidth=1, relief="solid", wraplength=100, justify="left").grid(column=i, row=c, sticky=N+S+E+W, rowspan=2) 
    def leave(self, var): 
     self.var = var 
     self.top.destroy() 

root = Tk() 
App(root) 
root.mainloop() 
+0

@Zitrone J'ai placé une boucle for à l'intérieur de 'enter()'. Cette boucle for est ce qui remplit le contenu de la fenêtre 'Toplevel'. Si vous remplacez la boucle for par l'extrait que vous souhaitez utiliser pour dessiner votre table, cela devrait fonctionner. –

+0

Ahh ok je comprends, ce n'est pas exactement ce que je voulais faire, mais j'ai réussi à le changer un peu et il semble que cela pourrait fonctionner, alors merci beaucoup. – Zitrone

+0

Qu'est-ce que vous vouliez faire, ce qui est différent? Si nous pouvons mettre à jour cela pour être une meilleure réponse, il est plus utile pour les personnes qui ont ce problème à l'avenir. –