can anyone explain why it is not possible to use tk.Entry textvariable if you have multiple GUIs in Python?
Imagine this:
I have a program that opens a GUI where a User can choose a file and that file with the filepath is getting set as the textvariable. This variable is getting set 100% correctly because if I tell my program to execute this GUI part then my Entry gets filled with the textvariable that the user chooses.
NOW why does it not work if I have a GUI that calls this (above) GUI with a button?
The values are not changed it's just 1 more GUI that gets called before the one.
SO this needs to be a problem with Python in general I think - does anyone know a workaround?
Some code:
# Data
catalog_file_path = StringVar()
# Functions
def getCatalogPath():
global catalog_file
catalog_file = filedialog.askopenfile(
mode='r', filetypes=[("Text files", "*.txt")])
catalog_file_path.set(catalog_file.name)
catalog_file.reconfigure(encoding='cp1252')
!! if I print here the value is correct !!
return catalog_file
# File-Path Textbox
textBox_catalog_file_path = Entry(
gui, textvariable=catalog_file_path, width=50)
textBox_catalog_file_path.config(font=guiFont)
textBox_catalog_file_path.grid(row=0, column=1, padx=5, pady=15)
Notice that the code does generally work but the textvariable just doesn't get shown at least if there is a GUI calling the Gui - so multiple GUIs basically. Is there something like a refresh element Funktion or something?
Thanks, Faded.
Related
I'm completely new to Tkinter and I can't seem to make it work, even when copy/pasting codes from tutorials. More specifically, the following code for instance
Mafenetre = Tk()
Button(Mafenetre, text = 'quit.', command = Mafenetre.destroy).pack()
v = StringVar()
v.set("New Text!")
Label(Mafenetre, relief='solid', textvariable=v).pack()
Mafenetre.mainloop()
does not show New Text (but does show the 'quit' button). More generally, any use I've made (even copy/pasted code) of the textvariable attribute does not produce any text. What am I not understanding ?
Thank you in advance
Tkinter variables need a tk instance. So use:
v=StringVar(Mafenetre)
I'm pretty new in programming, so I don't know many basics. I tried searching everywhere, but didn't get the answer I need.
In this website I found a similar problem, but it was for Python3.
I could change to python 3 interpreter, but then I would have to rewrite the code, due to syntax.
Anyway, my problem is, I want to write down text in a textbox and I need it to be taken for use (for example print it out or use it as name in a linux command).
I tried raw_input, even tried to add .get commands.
.get didn't work for me and input or raw_input input would do nothing, they would not print out the text and my programming gets stuck
My code
def filtras():
root = Tk()
root.title("Filtravimas pagal uzklausa")
root.geometry("300x100")
tekstas = Text(root, height=1, width=15).pack(side=TOP)
virsus = Frame(root)
virsus.pack()
apacia = Frame(root)
apacia.pack(side=BOTTOM)
myg1 = Button(virsus, text="Filtruoti", command=lambda: gauti())
myg1.pack(side=BOTTOM)
def gauti():
imti=input(tekstas)
print(imti)
Your problem is a common mistake in this line:
tekstas = Text(root, height=1, width=15).pack(side=TOP)
Geometry methods such as .pack and .grid return None, and None.get does not work. Instead do the same as you did for Frame and Button.
tekstas = Text(root, height=1, width=15)
tekstas.pack(side=TOP)
Now tekstas.get() and other Text methods will work.
This code does not touch on 2 <=> 3 syntax changes. The only issue is the name Tkinter versus tkinter and other module name changes.
Please read about MCVEs. Everything after the Text widget is just noise with respect to your question.
input and raw_input get characters from stdin, which is normally the terminal. Except for development and debugging, do not use them with GUI programs, and only use them if running the GUI program from a terminal or IDLE or other IDE. Ditto for print.
I have some pretty simple code right now that I am having issues with.
root = Tk()
label1 = Label(root, text ="Enter String:")
userInputString = Entry(root)
label1.pack()
userInputString.pack()
submit = Button(root,text = "Submit", command = root.destroy)
submit.pack(side =BOTTOM)
root.mainloop()
print(userInputString)
When I run the code everything operates as I would expect except
print(userInputString)
for an input asdf in the Entry print will return something like 0.9355325
But it will never be the same value back to back always random.
I am using python 3.5 and Eclipse Neon on a Windows 7 Machine.
Ultimately the goal is to accept a string from the user in the box that pops up and then be able to use that value as string later on. For example, it might be a file path that needs to be modified or opened.
Is Entry not the correct widget I should be using for this? Is there something inherently wrong with the code here? I am new to python and don't have a lot of strong programming experience so I am not even certain that this is set up right to receive a string.
Thanks in advance if anyone has any ideas.
There are two things wrong with your print statement. First, you print the widget, not the text in the widget. print(widget) prints str(widget), which is the tk pathname of the widget. The '.' represents the root window. The integer that follows is a number that tkinter assigned as the name of the widget. In current 3.6, it would instead be 'entry', so you would see ".entry".
Second, you try to print the widget text after you destroy the widget. After root.destroy, the python tkinter wrapper still exists, but the tk widget that it wrapped is gone. The following works on 3.6, Win10.
import tkinter as tk
root = tk.Tk()
label = tk.Label(root, text="Enter String:")
entry = tk.Entry(root)
def print_entry(event=None):
print(entry.get())
entry.bind('<Key-Return>', print_entry)
entry.focus_set()
submit = tk.Button(root, text="Submit", command=print_entry)
label.pack()
entry.pack()
submit.pack()
root.mainloop()
Bonus 1: I set the focus to the entry box so one can start typing without tabbing to the box or clicking on it.
Bonus 2: I bound the key to the submit function so one can submit without using the mouse. Note that the command then requires an 'event' parameter, but it must default to None to use it with the button.
The NMT Reference, which I use constantly, is fairly complete and mostly correct.
I'm currently trying to make a small application with a GUI that pulls weather from a website and displays the results in a window. I've got it to work without the GUI and also with the GUI but when I wrote the latter it was all in one script and not very organized. Because it was so unorganized, I decided to make make a separate script that would draw the GUI when the class was called.
Part of the GUI is an 'Entry' box that can be added via Tkinter. The entry box stores it's content into a StringVar() and that content can displayed using .get(). This works fine and well when I wrote everything unorganized into one script but I can't for the life of me figure out how to pass this StringVar() from one method to another in my program. This is what it looks like:
from Tkinter import *
import Forecast
class Frames(object):
def __init__(self):
pass
def main_frame(self):
main = Tk()
main.title('WeatherMe')
main.geometry('300x100')
query = StringVar()
Label(main, text='Enter a city below').pack()
Entry(main, textvariable=query).pack()
Button(main, text="Submit", command=self.result_frame).pack()
main.mainloop()
def result_frame(self):
result = Tk()
result.title('City')
result.geometry('600x125')
Button(result, text="OK", command=result.destroy).pack()
result.mainloop()
Basically my goal is to have one window open when the program is launched with a label, an entry box, and a submit button. When a city is entered in the entry box and submit it clicked a new window will open displaying the results.
Because the entry is on the first window I need to pass the value of entry's StringVar() to the second window so it can then pull the data and display the labels. No matter what I try it doesn't seem to work, I either get a 404 error meaning something is wrong with that string making the link it tries to get a response from invalid or a concatenate error 'cannot concatenate str and instance objects'.
I've also tried saving StringVar() as a variable outside of either method but the issue with that is I need to then call another instance of Tk() before StringVar().
You are creating two separate instances of Tk. You shouldn't do that. One reason why is because of this exact problem: you can't share instances of widgets or tkinter variables between them.
If you need more than one window, create a single root window and then one or more instances of Toplevel. Also, call mainloop only for the root window.
I am trying to create a simple popup text entry for the user in which a user enters a text and hits submit (a button). Upon clicking submit, I want the popup entry box to close off and continue on with the rest of the code. Following is a sample code for display that I borrowed from an old post here:
from Tkinter import *
root = Tk()
nameLabel = Label(root, text="Name")
ent = Entry(root, bd=5)
def getName():
print ent.get()
submit = Button(root, text ="Submit", command = getName)
nameLabel.pack()
ent.pack()
submit.pack(side = BOTTOM)
root.mainloop()
print "Rest of the code goes here"
I don't have much experience with Tkinter so I am not sure where and how exactly to call the appropriate functions for closing the entry box after the user hits 'Submit'. My guess is it would have to be inside the getName() function?
If I understand you correctly, then all you need to do is call the root window's destroy method at the end of the getName function:
def getName():
print ent.get()
root.destroy()
Doing so is equivalent to manually clicking the X button in the corner of the window.
Alternate method:
since there isn't much to your popup you could also eliminate several lines of code in your GUI, save some CPU and get pretty much the same output with this:
submitvariablename=raw_input('Please enter a Name')
same functionality and much faster, cleaner.
Just a thought.