Python-Tkinter Text Insert not affecting box - python

First time poster, found the site very helpful before registering though.
I am having issues using Tkinter on Python 2.7 (Windows7):
The code (I have truncated it because the whole thing is massive) looks something like this:
-------------------------------------------------------
CODE:
#set up stuff, importing variables, etc, then we have:
class App:
global RXSerial
RXSerial=''
#The following lines define the topFrame, lays out the widgets.
def __init__(self, master):
topFrame = Frame(master)
topFrame.pack()
middleFrame = Frame(master)
middleFrame.pack()
#--------------defining state variables------------
self.inputConsole = Text(middleFrame)
self.inputConsole.insert(INSERT,"Data recieved from Serial:")
self.inputConsole.config(width=100,height=20)
self.inputConsole.pack(side=LEFT,padx=20,pady=20)
#blah blah blah, insert a bunch of stuff (buttons etc.) here:
#The following lines define the functions to be called when the buttons are pressed.
def engineFire(self,engineUse,pwm):
RXSerial='this should pop up in the text called inputConsole'
print RXSerial
self.inputConsole.insert(INSERT, RXSerial)
---------------------------------------------------
so yeah, basically RXSerial is a string (that I have checked that is working, the print RXSerial line successfully prints when called by a button. The problem is that the self.inputConsole.insert(INSERT,RXSerial) line is not working. Can anybody please help? I have tried a bunch of combinations of stuff but cant seem to get it working. Thank you.

If you're trying to insert the text from another thread it may fail to work. Also, if at some point you configured the text widget to be in the disabled state then inserting will fail. If that's the case (widget is disabled), setting the state to "normal" temporarily will solve the problem.
Without more information it's impossible to say for sure.

Related

Using tkinter's button and entry widgets to update instance variable

I am trying to implement a little application that logs in a user and the user after log in can add/update/delete contents to/of a textfile. Here's a rough sketch of the code I have so far:
class admin():
def __init__(self):
self.app = Tk()
.
self.name=StringVar()
update = Button(self.app,....,command=self.update)
.
.
.
def update():
#Function to take different entries using Entry widget of tkinter in another window
anotherapp = Tk()
nameentry = Entry(anotherapp,textvariable = self.name)
submitbutton = Button(anotherapp,....,command=submit)
.
.
def submit():
#Opens a file and adds entries to a textfile.
namevar = self.name.get()
# code to append to file
To explain the above, I have a class admin. Creating an instance of this class would open a window with buttons that say create, update, delete and so on. On clicking one of the buttons, the respective functions (defined in the same class) would be called (I use lambda: in case the function has arguments, but so far, it doesn't).
So in the code I've mentioned, say I click on the Update button, it should call the update function which opens another window and takes the text that has to be updated in the text file (via the Entry widget). So according to the code it'll update the value of name. On clicking submit, the function submit uses .get() to get the string value of name, and appends it to the text file.
The code executes with no error but it DOES NOT read the input from the user. Blank lines get appended to my textfile when I click on the submit button.
Now I don't understand why this isn't working. The name variable is defined in init and can be updated by the functions of the same class. I have tried a lot of things to make this work, including adding parameters in the button commands, defining name elsewhere, etc. Even though I've solved the error, I get the same result: the file gets appended with blank lines. I've also tried to make name a class variable but that doesn't work since it is declared using StringVar() which needs it to be part of a tkinter window. I think I've also tried nesting submit function inside the update function, but I don't know why that didn't work out or if I hadn't implemented it correctly.
I don't know if it has to do with the working of tkinter's StringVar() and .get() function.
I can't think of any other way to implement the situation I have at hand. I am open to taking suggestions on changing the structure of the code, as long as it is not something major major and manages to achieve the functionality that I've described.
I am sorry if I've missed something basic, cause I've only started trying out OOP in python recently. And thanks in advance for any help.

How can I prompt user for multiple inputs, then print summary to a file?

So far I'm able to print at the end if the user selects 'n' to not order another hard drive, but need to write to a file. I've tried running the code as 'python hdorders.py >> orders.txt', but it won't prompt for the questions; only shows a blank line and if I break out using Ctrl-C, it writes blank entries and while loops in the file. I hope this makes sense.
ui = raw_input("Would you like to order more hard drives?(y/n) ")
if ui == 'n':
print '\n','\n',"**** Order Summary ****",'\n',row,'\n',"Number of HD's:",b,'\n',"Disk Slot Position(s):",c,'\n',"Disk Size(s):",d,"GB",'\n',"Dimensions:",e,'\n','\n',
endFlag = True
I'd also like it so that if they select 'y', it will save to a file and start over for another disk order (saving the previous info to the file first). Then once they are done (for example going through the program twice) and select 'n', it will have the final details appended to the same file as the first order.
I've found that when extensive user input is desired, a GUI may be the best option. I only try to do command line input if my script uses a small amount of user inputs that I can argparse out. Personally, I would make a tkinter combobox for each of these inputs and have a button at the bottom of the GUI that processes all the inputs and writes them to a file. Here is a skeleton of how I make a GUI
import tkinter as tk
class OOP:
def __init__(self):
self.win = tk.Tk()
self.win.title("My Title")
self.user_input = tk.StringVar()
self.create_widgets()
def lookup_csv_file(self):
file = self.user_input.get()
print(file)
def create_widgets(self):
tk.Button(self.win, text="Lookup CSV file", width=42, command=self.lookup_csv_file).pack(expand=1, fill='both')
tk.Entry(self.win, textvariable=self.user_input).pack(expand=1, fill='both')
app = OOP()
app.win.mainloop()
This code shows several important things to note:
1) GUI's should be made with Object Oriented Programming (OOP) for most cases
2) The variables you want to keep should be initialized in the __init__ section as tk.StringVar(), tk.IntVar(), etc.. and then attached to GUI sections (as seen in create_widgets(self): section the entry's text variable is attached to our varible
3) To access the variable you use its .get() method as seen in lookup_csv_file section. As well the variable has a .set() method if you would like to put a value there. For instance you can do self.user_input = tk.StringVar() followed with self.user_input.set('Default CSV file') and the GUI will initialize with that shown.
4) When assigning commands to buttons, do not include the (). If instead of command=self.lookup_csv_file you put command=self.lookup_csv_file() the command will run during initialization.
These are some of the finer points that were hard for me to learn, but with this you should be able to quickly learn by looking at the documentation available!
I've tried running the code as 'python hdorders.py >> orders.txt', but it won't prompt for the questions
You don't see the prompts since you redirect the standard output, whereto also the prompts go, to the file orders.txt. Better open the file within your program, without redirection at the shell:
if ui == 'n':
orders = open('orders.txt', 'a') # 'a' for appending
print >>orders, '\n','\n',"**** Order Summary ****",'\n',row,'\n',"Number of HDs:",b,…
orders.close()
- run as python hdorders.py.

page GUI - Combobox pass variable

New to GUI. Not quite getting there. I used page and get can get buttons to do something (click on a button and get a response). With Combobox, I can't pass a value. Searched here, tried many things, watched a few hours of youtube tutorials.
What am I doing wrong below? This is the code page generates (basically) then I added what I think I need to do to use the Combobox.
I am just trying to have 1,2,3 in a combo box and print out the value that is chosen. Once I figure that out I think I can actually make a simple GUI that passes variables I can then program what I want to do with these variables being selected.
class New_Toplevel_1:
def __init__(self, top):
self.box_value = StringVar()
self.TCombobox1 = ttk.Combobox(textvariable=self.box_value)
self.TCombobox1.place(relx=0.52, rely=0.38, relheight=0.05, relwidth=0.24)
self.TCombobox1['values']=['1','2','3']
self.TCombobox1.configure(background="#ffff80")
self.TCombobox1.configure(takefocus="")
self.TCombobox1.bind('<<ComboboxSelected>>',func=select_combobox)
def select_combobox(self,top=None):
print 'test combo ' # this prints so the bind works
self.value_of_combo = self.ttk.Combobox.get() # this plus many other attempts does not work
It's hard to know what you're actually asking about, since there is more than one thing wrong with your code. Since you say the print statement is working, I'm assuming the only problem you have with your actual code is with the last line.
To get the value of the combobox, get the value of the associated variable:
self.value_of_combo = self.box_value.get()
Here's a working version where I fixed the other things that were wrong with the program:
from tkinter import *
from tkinter import ttk
class New_Toplevel_1:
def __init__(self, top):
self.box_value = StringVar()
self.TCombobox1 = ttk.Combobox(textvariable=self.box_value)
self.TCombobox1.place(relx=0.52, rely=0.38, relheight=0.05, relwidth=0.24)
self.TCombobox1['values']=['1','2','3']
self.TCombobox1.configure(background="#ffff80")
self.TCombobox1.configure(takefocus="")
self.TCombobox1.bind('<<ComboboxSelected>>',func=self.select_combobox)
def select_combobox(self,top=None):
print('test combo ') # this prints so the bind works
self.value_of_combo = self.box_value.get()
print(self.value_of_combo)
root = Tk()
top = New_Toplevel_1(root)
root.mainloop()
Note: I strongly advise you not to start with place. You should try to learn pack and place first. I know place seems easier, but to get the most responsive and flexible GUI you should leverage the power of pack and grid.

What is causing this cascade menu failure with tkinter in python?

The following code exhibits a problem I do not understand:
from Tkinter import *
root = Tk()
cheese_var = IntVar()
parrot_var = IntVar(value=1)
check_menu = Menu(tearoff=0)
check_menu.add_checkbutton(label="Cheese", variable=cheese_var)
check_menu.add_checkbutton(label="Parrot", variable=parrot_var)
count = 0
class Top():
def __init__(self):
global count
count += 1
self.tl = Toplevel(root)
Label(self.tl, text="Window " + str(count)).pack()
self.mb = Menubutton(self.tl, text="Push Me", bg='pink')
self.menu = Menu(self.mb, tearoff=0)
self.menu.add_cascade(label="Choices", menu=check_menu)
self.menu.add_command(label="New Window", command=new_top)
self.mb.config(menu=self.menu)
self.mb.pack()
def new_top():
Top()
Top()
root.mainloop()
The menu brought up by the menu button in the created top level window initially behaves as expected. Clicking on the New Window command there creates a new such window, which also behaves as expected. Indeed, as long as you keep creating new top level windows, everything continues to work as expected. However, once you delete (close) any one of those windows, then, in a subsequently created new window, the Choices cascade on the new menu is not functional. (It is still OK in the windows created before the closing of one.)
The situation in which I initially encountered this symptom was much more complex, but I was able to simplify it down to the above example which exhibits the issue. I have discovered that I can avoid the problem by having each instance of Top create its own check_menu as an attribute; but I do not understand why this should be necessary. Please point me the way if there is one to avoid the problem without such replication of a cascade menu used in multiple windows.
Unfortunately, I don't think it is possible to do what you want. I'll try to explain as best as I can.
When you first run the script, check_menu is created and works fine for the first window. As you create more windows, check_menu is simply shared between them. However, when you close one of them, check_menu (and everything under it) is destroyed. So, when you create a new window after that, check_menu no longer exists and it doesn't show.
However, the script doesn't throw an error because, for some reason, Tkinter allows you to assign menus to things that aren't menus. Believe it or not, none of the following code:
self.menu.add_cascade(label="Choices", menu=None)
self.menu.add_cascade(label="Choices", menu=1)
self.menu.add_cascade(label="Choices", menu="")
will break the script. Each line simply does nothing but create an empty cascade "Choices".
That is basically what is happening. After closing one window, check_menu and everything under it is destroyed. Yet, Tkinter doesn't throw an error but instead assigns a menu to something that is no longer a menu (as far as what it is assigning the menu to, I believe it is using the old instance of check_menu, which was destroyed).
To solve this problem, recreate check_menu and everything under it each time you call Top. In other words, put the code for check_menu (and its options) in the __init__ method of Top. That way, each time Top is called, check_menu will exist.
Hope this helps (and that I explained it sufficiently :)

Hyperlink in Tkinter Text widget?

I am re designing a portion of my current software project, and want to use hyperlinks instead of Buttons. I really didn't want to use a Text widget, but that is all I could find when I googled the subject. Anyway, I found an example of this, but keep getting this error:
TclError: bitmap "blue" not defined
When I add this line of code (using the IDLE)
hyperlink = tkHyperlinkManager.HyperlinkManager(text)
The code for the module is located here and the code for the script is located here
Anyone have any ideas?
The part that is giving problems says foreground="blue", which is known as a color in Tkinter, isn't it?
If you don't want to use a text widget, you don't need to. An alternative is to use a label and bind mouse clicks to it. Even though it's a label it still responds to events.
For example:
import tkinter as tk
class App:
def __init__(self, root):
self.root = root
for text in ("link1", "link2", "link3"):
link = tk.Label(text=text, foreground="#0000ff")
link.bind("<1>", lambda event, text=text: self.click_link(event, text))
link.pack()
def click_link(self, event, text):
print("You clicked '%s'" % text)
root = tk.Tk()
app = App(root)
root.mainloop()
If you want, you can get fancy and add additional bindings for <Enter> and <Leave> events so you can alter the look when the user hovers. And, of course, you can change the font so that the text is underlined if you so choose.
Tk is a wonderful toolkit that gives you the building blocks to do just about whatever you want. You just need to look at the widgets not as a set of pre-made walls and doors but more like a pile of lumbar, bricks and mortar.
"blue" should indeed be acceptable (since you're on Windows, Tkinter should use its built-in color names table -- it might be a system misconfiguration on X11, but not on Windows); therefore, this is a puzzling problem (maybe a Tkinter misconfig...?). What happen if you use foreground="#00F" instead, for example? This doesn't explain the problem but might let you work around it, at least...

Categories

Resources