0

I'm trying to make an GUI to launch Ansible playbook, I've done a main app container with a menu to swicth between different frame.

The main.py looks like this:

#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
import Tkinter as tk
import sys, os, subprocess, json
from Tkinter import Menu
import ttk
#import des classe frame
from classes_tkinter.launcher import *

#Application principale
class MainApp(tk.Tk):

    def __init__(self):
        #Titre de l'application
        tk.Tk.__init__(self)
        self.version = "1.0.0"
        self.title("Playbook Launcher v"+self.version)
        self.geometry("1200x950")
        
        #On fait le menu
        self.init_menu()

        #On paramètre les frames
        self.container = tk.Frame(self)
        self.container.pack(fill="both", expand=True)
        #listes des frames
        self.frames = {}

        for f in (CopyScript,Reboot):
            name = f.__name__
            frame = f(parent=self.container, controller=self)
            self.frames[name] = frame
            frame.grid(row=0,column=0,sticky="nsew")
            
        self.show_frame("CopyScript")

    def init_menu(self):
        menubar = Menu(self)
        
        #Menu pour lancer des Playbook
        menu_playbook = Menu(menubar,tearoff=0)
        menu_playbook.add_command(label="Copier fichier",command=lambda: self.show_frame(page_name="CopyScript"))
        menu_playbook.add_command(label="Redémarrage",command=lambda: self.show_frame(page_name="Reboot"))
        
        #Menu pour faire des ajouts
        menu_ajout = Menu(menubar,tearoff=0)
        menu_ajout.add_command(label="Ajout playbook")
        menu_ajout.add_command(label="Ajout host")

        #Menu pour voir les informations stockées en base
        menu_consult = Menu(menubar,tearoff=0)
        menu_consult.add_command(label="Playbook")
        menu_consult.add_command(label="Inventory")

        #Ajout dans le menu
        menubar.add_cascade(label="Lanceur Playbook",menu=menu_playbook)
        menubar.add_cascade(label="Ajouts",menu=menu_ajout)
        menubar.add_cascade(label="Consultation",menu=menu_consult)

        #Ajout dans l'application
        self.config(menu=menubar)

    def show_frame(self,page_name):
        frame = self.frames[page_name]
        frame.tkraise()

if __name__ == "__main__":
    MainApp().mainloop()

and for example one of the frames loaded looks like this, launcher.py:

import Tkinter as tk
import sys, os, subprocess, json
from database.database_helper import *
import ttk

class CopyScript(tk.Frame):
    def __init__(self,parent,controller):
        tk.Frame.__init__(self,parent)
        self.controller = controller
        #Titre de la frame
        self.label =  ttk.Label(self ,text="Copier fichier",font=("Arial", 14))
        self.label.grid(row=0,column=1,padx=550)

        #Widget pour les paramètres
        #choix de l'inventaire
        self.inventaire_labelframe = ttk.LabelFrame(self,text="Inventaire")
        self.targetmachine_labelframe = ttk.LabelFrame(self,text="Machines ciblée")
        environnement = []
        for r in Inventory().all_data:
            environnement.append(r[1])
        listbox = ttk.Combobox(self.inventaire_labelframe,values=environnement)
        listbox.pack()

        #pour la cible des machines
        
        self.inventaire_labelframe.grid(row=1,column=1 ,sticky='W') #,padx=5, pady=5, ipadx=5, ipady=5)
        self.targetmachine_labelframe.grid(row=1,column=2,sticky='W') # sticky='W',padx=10, pady=5, ipadx=10, ipady=5)

My problem is that self.inventaire_labelframe is well displayed but not the self.targetmachine_labelframe and I don't understand why.

I searched in the documentation but I haven't found a solution. Maybe there is an order in the grid?

8
  • It is because you have padx=550 on self.label.grid(...), so the second labelframe is pushed out of the viewable area of the window. If you set padx to a smaller value, you can see the second labelframe. Commented Oct 1, 2024 at 15:44
  • Hum okay but if i dont put padx=550 , the label with text = "Copier Fichier" will not centered ? Commented Oct 2, 2024 at 5:44
  • 1
    You can try padx=(550,0) instead. However using padx to center a label is not a good design. Commented Oct 2, 2024 at 5:59
  • :) I'll like to have a great method to center this widget without this :) Commented Oct 2, 2024 at 6:30
  • Well even after i remove padx , my second label frame doesnt appear in my app window :( Commented Oct 2, 2024 at 9:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.