Python Forum
Inter-window variables?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Inter-window variables?
#1
I am trying to make a GUI to have choices on what action to do to fill another window. I can get the window to work with hard-coded options (and a different file for each option choice), but would prefer to use a GUI (without having it be a selector for which specialized python file to run) to select the options.

Here is a simplified example (I add .01 to the value because for the full thing I would like to do a variety of functions with the numerical values and modify the text to similar strings and then use that as an option selection, both of which work fully, when hardcoded).
from tkinter import *

root = Tk()
root.lift()
root.title('Test')
root.geometry('500x100')

Number = Label(root)
Number.pack()
numeral = StringVar()
Number['textvariable'] = numeral
numeral.set('Nothing has been entered into the GUI')


GUI = Tk()
GUI.lift()
GUI.title('GUI')
GUI.geometry('300x100')

num = StringVar()
numBox = Entry(GUI, textvariable=num)
numBox.pack()


def Load():
    while 1:
        try:
            Fill = (int(num.get()) + .01)
        except Exception:
            try:
                Fill = (int(str(num.get())) + .01)
            except Exception:
                try:
                    Fill = str('{}?'.format(num.get()))
                except Exception:
                    pass

        try:
            numeral.set('{} is entered into the GUI'.format(Fill))
        except Exception:
            numeral.set('Nothing has been entered into the GUI')
        root.update()

root.after(100, Load)
root.mainloop()
Currently, I am hard-coding it by using .set for the hard-coded value desired immediately following the initial occurrence of the tkinter variable. I have tried making the main window into a function with the value in the label a parameter of the function, and the GUI to have a button to close it and call the function to make the other window, but it also only works if the values are hard-coded. I have tried putting each variable through a middle-man global (or make the tkinter variable global and then have a local middle-man variable). I used the simplest code that I do not understand the flaw with in the example.
Reply
#2
IF you want the label to show what was entered in the Entry, then use the same StringVar(), numeral, for both. If not, post back with what you want the program to do as no where in your post can I find that. How to get the contents of an Entry (note the callback) http://effbot.org/tkinterbook/entry.htm
Reply
#3
I do not want to have the label to be a copy of the entry; I want it to modify itself based on what is in the entry. I tried replacing num with numeral, yet it still does not work any differently. If I replace all instances of num with numeral, then I get something similar to the Droste effect, increasing the same amount each .1 seconds.
Reply
#4
Quote:I want it to modify itself based on what is in the entry.
If you aren't going to read what I post then there is no point in posting.
Reply
#5
(Jun-30-2018, 09:40 PM)woooee Wrote:
Quote:I want it to modify itself based on what is in the entry.
If you aren't going to read what I post then there is no point in posting.

I did read what you posted. I do not want the main window to simply show what is in the entry, but do some things with the entry (such as convert certain strings into numbers or vice versa, or convert it to another unit). I do not understand how that is the same as simply showing what is in the entry. I do not want the two widgets' values to be binded, I want the value of the Entry to go through functions, and then the function to return a value for another variable which will be the output listed in the main window. Sorry if I am/was unclear.
I tried what you suggested, but it did not function any differently.

I can simply get the contents of the entry and use them however I like in the same window by adding
l = Label(GUI, textvariable=num)
l.pack()
I understand how to do that, and that I can use the same variable if I want to have an identical content in each widget but I wish to have something different (although similar/related to/derived from) in the two widgets, in different windows.
from tkinter import *

##root = Tk()
##root.lift()
##root.title('Test')
##root.geometry('500x100')
##
##Number = Label(root)
##Number.pack()
##numeral = StringVar()
##Number['textvariable'] = numeral
##numeral.set('Nothing has been entered into the GUI')


GUI = Tk()
GUI.lift()
GUI.title('GUI')
GUI.geometry('300x100')

num = StringVar()
numBox = Entry(GUI, textvariable=num)
numBox.pack()

num2 = StringVar()
l = Label(GUI, textvariable=num2)
l.pack()


def Load():
    while 1:
        try:
            Fill = (int(num.get()) + .01)
        except Exception:
            try:
                Fill = (int(str(num.get())) + .01)
            except Exception:
                try:
                    Fill = str('{}?'.format(num.get()))
                except Exception:
                    pass

        try:
            num2.set('{} is entered into the GUI'.format(Fill))
        except Exception:
            num2.set('Nothing has been entered into the GUI')
        GUI.update()

GUI.after(100, Load)
GUI.mainloop()
☝ Gives the desired result, except that it is in the same window, which I would like to avoid.
Reply
#6
I cannot edit the main post, but I have solved this by making the GUI use Toplevel() instead of Tk()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 346 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  [Tkinter] Where to best ask for feature requests for TK(inter) NebularNerd 2 1,089 Feb-10-2023, 12:34 PM
Last Post: NebularNerd
  tkinter window and turtle window error 1885 3 6,625 Nov-02-2019, 12:18 PM
Last Post: 1885
  update a variable in parent window after closing its toplevel window gray 5 8,978 Mar-20-2017, 10:35 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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