Python Forum
[Tkinter] Tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Tkinter
#1
could someone help me with some part of a GUI? There is a submenu, Load, upon clicking, a fileselector dialog should pop up. The file has three variables, (Name, Subject, Mark) a row, separated by commas. The names and subjects are stored in Label typed tkinter objects, marks are displayed in Entries. At the bottom, a button should be placed . If clicked, it should disable/enable all the Entry typed objects in the gui. I am done with this so far:

from tkinter import *
from tkinter import messagebox
from tkinter import filedialog
from tkinter import ttk


def loadthefile():
    filename = filedialog.askopenfilename()

    with open(filename) as f:
        rownum = sum(1 for _ in f)



    def my_callback():
        if button1["text"] == "Unlock":
            button1["text"] = "Lock"


        else:
            button1["text"] = "Unlock"


    button1 = Button(master, text= "Unlock", command=my_callback)
    button1.grid(row=rownum + 1, columnspan=3, sticky=W+E+S+N)



master = Tk()
menu_bar = Menu(master)
file_menu = Menu(menu_bar, tearoff=0)
file_menu.add_command(label="Load", command=loadthefile)
file_menu.add_command(label="Save")

file_menu.add_separator()

file_menu.add_command(label="Quit", command=master.quit)
menu_bar.add_cascade(label="File", menu=file_menu)


master.config(menu=menu_bar)
master.mainloop()

Thank you!
Reply
#2
from tkinter import *
import tkinter.messagebox as mb
# from tkinter import messagebox
from tkinter import filedialog
# from tkinter import ttk


def loadthefile():
    filename = filedialog.askopenfilename()

    if filename:
        with open(filename) as f:
            rownum = sum(1 for _ in f)
    else:
        mb.showerror('Filename', 'Please select a file')

    # def my_callback():
    #     if button1["text"] == "Unlock":
    #         button1["text"] = "Lock"
    #
    #
    #     else:
    #         button1["text"] = "Unlock"
    #
    # button1 = Button(master, text="Unlock", command=my_callback)
    # button1.grid(row=rownum + 1, columnspan=3, sticky=W + E + S + N)

def savethefile():
    print('Save menu item selected')

master = Tk()
menu_bar = Menu(master)
file_menu = Menu(menu_bar, tearoff=0)
file_menu.add_command(label="Load", command=loadthefile)
file_menu.add_command(label="Save", command=savethefile)

file_menu.add_separator()

file_menu.add_command(label="Quit", command=master.quit)
menu_bar.add_cascade(label="File", menu=file_menu)

master.config(menu=menu_bar)
master.mainloop()
Reply
#3
Thanks!
Could someone tell me how do I create Labels and entries based on the contents of the file? By that I mean I have three things separated by commas in a row, and these should be displayed as a sort of table, with three columns, and as many rows as the number of lines in the file.

Example:
name, age, gender
max, 5, m
molly, 6, f

This should have three colums, all labels, and 3 rows as well.
Reply
#4
use textvariable as one of the attributes:
it requires a StringVal variable entry.
variablename = StringVal()
this can be a complex string containing newlines between items
you can then 'set' this variable like (assuming python 3.6 or newer):
from file record, set name, age and gender, then
variablename.set(f'name: {name}\nAge: {age}\nGender: {gender}\n{}')
the textvariable will update the label
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020