Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14155

Building a Python Tkinter GUI without enumerated global variables

$
0
0

I want to have tabs in a notebook and have a way to enter something into a text box at the bottom of the screen and then it to be echoed back out to me. I know I'm on the right path and maybe having it all in separate functions isn't a good idea I was able to get the <return> event to work but just printed out to terminal and not the label within the new tab (think like Skype or another messaging app with tabs, that's basically what I am going for).

import tkinter as tkfrom tkinter import ttktCount=0def do_command(event):    text = Text(root)    text.pack()    tabControl.add(text, text=entry.get())def make_new_tab(event):    global tCount    global oLabel    global entry    if tabControl.select() == tabControl.tabs()[-1]:        tCount+=1        index = len(tabControl.tabs())-1        frame = tk.Frame(tabControl)        oLabel = tk.Label(frame)        entry_var = tk.StringVar()        entry = tk.Entry(frame, textvariable=entry_var,width=root.winfo_vrootwidth())        entry.bind("<Return>", lambda event: do_command)        entry.pack(side='bottom', anchor='w')        tabControl.insert(index, frame, text='tCount')        tabControl.select(index)def setup():    global tabControl    global root    root = tk.Tk()    root.geometry('500x500')    root.title("Tinkering With Tkinter")    tabControl = ttk.Notebook(root)    frame = tk.Frame(tabControl)    tabControl.add(frame, text="+")    tabControl.bind("<<NotebookTabChanged>>", make_new_tab)    tabControl.bind("<Configure>", )    tabControl.pack(expand=1, fill="both")    root.mainloop()if __name__ == "__main__":    setup()

I dislike the way I have the global variables. Is there some way to do this without using them like this? Maybe in an array?


Viewing all articles
Browse latest Browse all 14155

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>