Update config file read midway through code

82 Views Asked by At

I am trying to create a config file that essentially updates the "look" of the page after a code has been entered. If the correct number has been entered, read the "Page2" part of the config file to update the background color and text. This is a small version of my code. I get the 'page' string to successfully update from 'page1' to 'page2' but the program does not re-read the config file with the 'page2' update

CONFIG FILE:

[page1]
line1 = This is the first line of the program
background = black

[page2]
line1 = This is now the second line
background = blue

PYTHON PROGRAM:

from tkinter import *
import keyboard
import tkinter.font as TkFont
from configparser import ConfigParser

config1 = ConfigParser()
config1.read("config1.ini")


updatePage = 1
page = "page" + str(updatePage)

line1 = config1.get(page, 'line1')
background = config1.get(page, 'background')

root = Tk()
root.title("Main Entry")
root.attributes('-fullscreen',True)
root.configure(background=background)



T1 = Label(root, text = line1, fg = 'white', wraplength = "1000", justify = 'left', bg=background, font = ('Consolas 16')).grid(row=0,column=0, padx=40, pady=5,sticky = 'nw')
e = Entry(root, width=120, borderwidth=0, bg= background, justify = 'left', fg = 'white', font = ('Consolas 16'), insertontime=500, insertofftime=500, takefocus=1)
e.grid(row=5, column=0, columnspan=5, padx=40, pady=0, sticky = 'nw')
e.focus_set()

print("global page is: ", page)

def enter():
    global updatePage
    global page
    print("i'v entered")
    current = e.get()
    if current == '4565':
        print("match")
        updatePage += 1
        page = "page" + str(updatePage) 
        print("page is: ", page)
        e.delete(0, END)
        config1.read
        return
    else:
        return

print('ive exited')


check = root.bind('<Return>', lambda event:enter())

print("global page is: ", page)

root.mainloop()

I want each key in the config to be setup the same (page 1 has background, line1, page 2 has background, line2) then I can just update the config1.read info to reread the config file. I've tried separate configs, different methods. Just trying to avoid making a separate line for every key because the end goal is a lot of config info.

2

There are 2 best solutions below

1
toyota Supra On

If the correct number has been entered, read the "Page2" part of the config file to update the background color and text.

Split two line.

T1 = Label(root, text = line1, fg = 'white', wraplength = "1000", justify = 'left', bg=background, font = ('Consolas 16'))
T1.grid(row=0,column=0, padx=40, pady=5,sticky = 'nw')

In line 39-43 Add this in enter() function.

page = "page" + str(updatePage)
line1 = config1.get(page, 'line1')
background = config1.get(page, 'background')
T1.configure(text=line1)
T1.configure(bg=background)

Code:

def enter():
    global updatePage
    global page
    print("i'v entered")
    current = e.get()
    if current == '4565':
        print("match")
        updatePage += 1
        page = "page" + str(updatePage)
        line1 = config1.get(page, 'line1')
        background = config1.get(page, 'background')
        T1.configure(text=line1)
        T1.configure(bg=background)
        print("page is: ", page)
        e.delete(0, END)
        config1.read
        return
    else:
        return

Screenshot:

enter image description here

0
ryan jones On

This is what I came up with after some suggestions. Note that I was having issues with two windows overlaying each other. To avoid "seeing" the window in the back, I made it so it also creates new versions of root and gets rid of the previous window. This is also my first Python program a week into it so it's a bit sloppy and has some extraneous bits but works nonetheless.

from tkinter import *
import keyboard
import tkinter.font as TkFont
from configparser import ConfigParser
import time

config1 = ConfigParser()



updatePage = 1
page = "page" + str(updatePage)
root = "root" + str(updatePage)

def call_again():
    global config1, line1, background, page, root, T1, e, line2, oldroot
    print("call again updatePage is ", updatePage)
    print("also, root is...", root)
    
    config1.read("config1.ini")
    line1 = config1.get(page, 'line1')
    line2 = config1.get(page, 'line2')
    background = config1.get(page, 'background')
    print("label is now this: ", line1)
    
    root = Tk()
    root.configure(background=background)
    root.title("Main Entry")
    root.attributes('-fullscreen',True)
    root.configure(background=background)
    root.focus_set()
    check = root.bind('<Return>', lambda event:enter())
    
    T1 = Label(root, text = line1, fg = 'white', wraplength = "1000", justify = 'left', bg=background, font = ('Consolas 16')).grid(row=0,column=0, padx=40, pady=5,sticky = 'nw')
    T2 = Label(root, text = line2, fg = 'white', wraplength = "1000", justify = 'left', bg=background, font = ('Consolas 16')).grid(row=1,column=0, padx=40, pady=5,sticky = 'nw')
    
    e = Entry(root, width=120, borderwidth=0, bg= background, justify = 'left', fg = 'white', font = ('Consolas 16'), insertontime=500, insertofftime=500, takefocus=1)
    e.grid(row=5, column=0, columnspan=5, padx=40, pady=0, sticky = 'nw')
    e.focus_set()
    
    if updatePage > 1:
        print("old root: ", oldroot)
        oldroot.after(2000, oldroot.withdraw)

if updatePage == 1:
    call_again()
else:
    None



def enter():
    global oldroot, updatePage, page, root
    print("i'v entered")
    current = e.get()
    if current == '4565':
        print("match")
        oldroot = root
        updatePage += 1
        page = "page" + str(updatePage) 
        root = "root" + str(updatePage)
        e.delete(0, END)
        call_again()
        return
    else:
        return


root.mainloop()