mercredi 14 novembre 2018

tkinter display frame on click

I'm trying to display/include a function: def display_srv on a click from the menu bar command and get values of each checkbox selected with the button Done

import tkinter as tk
import glob
from tkinter import *

def frame_1():
    print("frame 1!")

The function i tried to display in the frame:

def display_srv():
    frame_1 = root.LabelFrame(root, text="Liste Serveurs")
    frame_1.grid(row=0, sticky='ew', padx=20, pady=20, ipadx=5, ipady=5)
    path = '/home/lst/*.txt'
    files=glob.glob(path)
    count = 0
    for file in files:
        with open(file, 'r') as lst_file:
            for item in lst_file:
                lng = root.Checkbutton(frame_1, variable = item, text=item.rstrip()).grid(row=count//10, column=count%10)
                count += 1

txt file value:

item1
item2
item3
...
item100

The function for get values:

def getvalue():
    print(list(lng.values()))

The main script:

root = Tk()
menu = Menu(root)
root.geometry('700x500')
root.title("My menu")
root.config(menu=menu)
testpsimenu = Menu(menu)
menu.add_cascade(label="Test Menu", menu=testpsimenu, font=("Arial", 12))
testpsimenu.add_command(label="Select 1", font=("Arial", 10), command=frame_1)
testpsimenu.add_separator()
testpsimenu.add_command(label="Select 2", font=("Arial", 10), command=display_srv)

psimenu = Menu(menu)
menu.add_cascade(label="PSI Real", menu=psimenu, font=("Arial", 12))
psimenu.add_command(label="Select 1", font=("Arial", 10), command=frame_1)
psimenu.add_separator()
psimenu.add_command(label="Select 2", font=("Arial", 10), command=frame_1)

exitmenu  = Menu(menu)
menu.add_cascade(label="Exit", menu=exitmenu, font=("Arial", 12))
exitmenu.add_command(label="Exit", command=root.quit, font=("Arial", 10))

Button(root, text='Done', font=("Arial", 12), command=getvalue).pack(side=RIGHT)

mainloop()

Many thanks for your help




Aucun commentaire:

Enregistrer un commentaire