My text isn't showing in the python Shell. Help! Uses Tkinter - python

Please help! It comes up with an error saying that there is a missing "Self" and I cant find it. I have tried everything and i cant find the error.
from tkinter import *
import tkinter
class App:
def __init__(self):
self.master = tkinter.Tk()
self.master.title("Encrypter & Decrypter")
def E_Entry(self):
print(self.E_Entry.get(self))
self.E_Question = Label(self.master, text="Encrypter",)
self.E_Question.grid(row=1, column=1, sticky=E)
self.E_Entry = Entry(self.master, width = 25)
self.E_Entry.grid(row=1, column=2)
self.E_Button = Button(self.master, text="Encrypt", command=E_Entry)
self.E_Button.grid(row=1, column=3)
self.D_Question = Label(self.master, text="Decrypter",)
self.D_Question.grid(row=2, column=1, sticky=E)
self.D_Entry = Entry(self.master, width = 25)
self.D_Entry.grid(row=2, column=2)
self.D_Button = Button(self.master, text="Decrypt")
self.D_Button.grid(row=2, column=3)
self.master.mainloop()
App()

You should not have a function with the same name as the widget, the indentation of the function is incorrect, and you are calling it incorrectly.
It should be something like this:
class App:
def __init__(self):
...
self.E_Button = Button(..., command=self.print_e)
...
def print_e(self):
print(self.E_Entry.get())

On the line where it says:
App()
You should have assigned the resulting object to a variable, such as:
my_app = App()

Related

tkinter combobox, Entry correctly show when run from same file. But does not show correct values when same is called from another module

when I call same function from within the file it shows correct values. The values are filled in same module. But when I call itenter code here from another module it does not show values in widgets. I have gone thru similar problems solved earlier but they are solved from within the module.( I am new to python but have worked for many years on visual basic I am in the process of porting my code to python.)
In similar problems somebody suggested app.wait_window. This helped in showing the values if called from same file. As suggested in other solution I am using Class.
demobill.py
import tkinter as tk
from tkinter import ttk
import datetime
class Bill(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.winfo_toplevel().title("BILL")
self.grid()
topfrm = tk.Frame(self)
topfrm.config(bg="yellow", height=350, width=500)
topfrm.grid(row=0, column=0, columnspan=1)
lbl1 = tk.Label(topfrm, text="Party :", bg="light green")
lbl1.grid(row=0, column=0)
self.vparty = tk.StringVar()
self.mParty=ttk.Combobox(topfrm,textvariable=self.vparty,width=20, values =('apple', 'banana', 'CranBerry', 'dogwood', 'alpha', 'Acorn', 'Anise' ))
self.mParty.state = 'readonly'
self.mParty.grid(row=0, column=1)
self.mregdt=tk.StringVar()
self.regdt = tk.Entry(topfrm, width=12, background='darkblue', foreground='white', borderwidth=2)
self.regdt['textvariable']=self.mregdt
self.regdt.grid(row=2, column=1)
lblbillno = tk.Label(topfrm, text="Bill No:")
lblbillno.grid(row=3, column=0, ipadx=10)
self.mbillno = tk.StringVar()
billno = tk.Entry(topfrm, textvariable= self.mbillno, width=16, justify=tk.LEFT)
billno.grid(row=3,column=1)
self.mdr = tk.DoubleVar()
self.mcr = tk.DoubleVar()
self.edr = tk.Entry(topfrm, textvariable= self.mdr, width=16, justify=tk.RIGHT)
self.edr.grid(row=4,column=1)
self.ecr = tk.Entry(topfrm, textvariable= self.mcr, width=16, justify=tk.RIGHT)
self.ecr.grid(row=5,column=1)
btnQuit = tk.Button(topfrm, text='Quit', padx=5, width=10, command=self.cmdQuit)
btnQuit.grid(row=10, column=3)
def cmdQuit(self):
self.destroy()
if __name__ == "__main__":
exit()
else:
self.master.destroy()
def bill_readfill(self,bill_id):
mregdt="2019-01-01"
self.mregdt.set(mregdt)
self.mbillno.set( 101)
self.vparty.set('banana')
self.mdr.set(1000)
# print (self.mdr.get())
self.mcr.set(100)
def billtest(bill_id):
root=tk.Tk()
root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(),root.winfo_screenheight()))
app = Bill(root)
app.bill_readfill(bill_id)
app.mainloop()
if __name__ == "__main__":
billtest(1)
democall.py
import tkinter as tk
import demobill
def runbill():
demobill.billtest(1)
if __name__ == "__main__":
root = tk.Tk()
btncolr = "#2255ff"
btnNew = tk.Button(root, text='bill', padx=5, bd=2,width=10, bg=btncolr, command=runbill)
btnNew.pack()
root.mainloop()
no error messages. The values are there. They can be printed from within. Just not shown in widget.

Tracing Entry that uses a StringVar has no effect on Label

Learning to use Tkinter and following an online tutorial. This is an example given where text is entered and then label will update accordingly to the input text field.
I'm trying it in Python3 on Mac and on Raspberry Pi and I don't see the effect of trace, hence the label doesn't get modified by the Entry. Any help would be appreciate (or any other simple example of how to use Entry and Trace together)
Thanks.
from tkinter import *
class HelloWorld:
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.button = Button(
frame, text="Hello", command=self.button_pressed
)
self.button.pack(side=LEFT, padx=5)
self.label = Label(frame, text="This is a label")
self.label.pack()
a_var = StringVar()
a_var.trace("w", self.var_changed)
self.entry = Entry(frame,textvariable=a_var)
self.entry.pack()
def button_pressed(self):
self.label.config(text="I've been pressed!")
def var_changed(self, a, b, c):
self.label.config(text=self.entry.get())
def main():
root = Tk()
root.geometry("250x150+300+300")
ex = HelloWorld(root)
root.mainloop()
if __name__ == '__main__':
main()
The problem is that you are using a local variable for a_var, and on the Mac it is getting garbage-collected. Save a reference to the variable (eg: self.a_var rather than just a_var).
self.a_var = StringVar()
self.a_var.trace("w", self.var_changed)
self.entry = Entry(frame,textvariable=self.a_var)
self.entry.pack()
Note: if all you want is to keep a label and entry in sync, you don't need to use a trace. You can link them by giving them both the same textvariable:
self.entry = Entry(frame, textvariable=self.a_var)
self.label = Label(frame, textvariable=self.a_var)

Python - Auto add widgets

So I am currently trying to create a button on a GUI that will let the user generate a new entry field.
I have no idea how to do this. I'm guessing that it will require a lambda function, but apart from that, I have no idea.
Here's the basic code I have so far:
from tkinter import *
class prac:
def autoAddWidget(self,frame,x,y):
self.entryField = Entry(frame,text="Entry Field")
self.entryField.grid(row=x, column=y)
#lambda function?
def __init__(self, master):
frame = Frame(master, width=60, height=50)
frame.pack()
x=1
self.addWidgetButton = Button(frame, text="Add new widget", command=self.autoAddWidget(frame, x,0))
self.addWidgetButton.grid(row=0, column=0)
x+=1
root = Tk()
app = prac(root)
root.mainloop()
Would appreciate the help.
Thanks
You're passing to the command argument result from the method self.autoAddWidget(frame, x,0) not method itself. You have to pass there a reference to a callable object, a function that will be called when the event occurs. Please check a documentation next time before you ask the question.
Ok, I fixed the code, now it works:
from tkinter import *
class Prac:
def autoAddWidget(self):
self.entryField = Entry(self.frame,text="Entry Field")
self.entryField.grid(row=self.x, column=0)
self.x+=1
def __init__(self, master):
self.frame = Frame(master, width=60, height=50)
self.frame.pack()
self.x=1
self.addWidgetButton = Button(self.frame, text="Add new widget", command=self.autoAddWidget)
self.addWidgetButton.grid(row=0, column=0)
root = Tk()
app = Prac(root)
root.mainloop()

tkinter script to print entry text

I'm just starting to learn to use tkinter with python and it seems counterintuitive that this attempt at a simple script to print whatever is entered into the entry box, doesn't work:
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
text_write = Entry(frame)
text_write.pack()
self.button = Button(frame, text="quit", fg="red", command=frame.quit)
self.button.pack(side=LEFT)
self.hi_there = Button(frame, text='hello', fg='black', command=self.say_hi(text_write.get()))
self.hi_there.pack(side=RIGHT)
def say_hi(self, text):
print(text)
root = Tk()
app = App(root)
root.mainloop()
This does nothing and outputs no errors, but if I change it to this:
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.text_write = Entry(frame)
self.text_write.pack()
self.button = Button(frame, text="quit", fg="red", command=frame.quit)
self.button.pack(side=LEFT)
self.hi_there = Button(frame, text='hello', fg='black', command=self.say_hi)
self.hi_there.pack(side=RIGHT)
def say_hi(self):
print(self.text_write.get())
then it calls the function and prints the value. Why does the 'self' need to be declared there? And why can't you pass the value of text_write as an argument to say_hi (as in the first example) and have it display? Or can you and I'm just doing it wrong?
When you do this:
self.button = Button(..., command=func())
then python will call func(), and the result of that will be assigned to the command attribute. Since your func doesn't return anything, command will be None. That's why, when you push the button, nothing happens.
Your second version seems fine. The reason self is required is that without it, text_write is a local variable only visible to to the init function. By using self, it becomes an attribute of the object and thus accessible to all of the methods of the object.
If you want to learn how to pass arguments similar to your first attempt, search this site for uses of lambda and functools.partial. This sort of question has been asked and answered several times.

How to correctly handle a confirmation event?

I am trying to create frame with 2 input areas (for login and password) and confirmation button (after this button is pressed - code will read areas values). But I don't know how to do it inside class App without using some global function.
from Tkinter import *
class App:
def __init__(self, master):
frame_credentials = Frame(master, width=100, height=200)
frame_credentials.pack()
self.label_login = Label(frame_credentials, text='login')
self.text_login = Entry(frame_credentials, width=15)
self.label_pass = Label(frame_credentials, text='password')
self.text_pass = Entry(frame_credentials, show="*", width=15)
self.button_ok = Button(frame_credentials, text="Login")
self.label_login.grid(row=0, column=0)
self.text_login.grid(row=1, column=0)
self.label_pass.grid(row=2, column=0)
self.text_pass.grid(row=3, column=0)
self.button_ok.grid(row=0, column=1, rowspan=4)
self.button_ok.bind("<Button-1>", enter_in)
def enter_in(self):
print self.text_login, self.text_pass
root = Tk()
app = App(root)
root.mainloop()
Don't bind to <Button-1>; instead, use the command attribute and give it the name of a method in your object. For example:
class App:
def __init__(...):
...
self.button_ok = Button(..., command=self.enter_in)
...
def enter_in(self):
<put your login logic here>

Categories