Scrolling an Entry widget on python - python-3.x

I try to make scrolling on an Entry in python.
when I run program nothing happen.
Please any one help me??
this is my code:
self.scrollbar = tk.Scrollbar(self,orient="horizontal")
self.e3 =tk.Entry(self,xscrollcommand=self.scrollbar.set)
self.e3.focus()
self.e3.pack(side="bottom",fill="x")
#self.e3.grid(row=10, column=7)
self.scrollbar.pack(fill="x")
self.scrollbar.config(command=self.e3.xview)
self.e3.config()

Your code works, just delete all "self". Word "self" is usually used in classes (can be replaced with anything, like "bananas"). Here you can find some explanation:
What is the purpose of self?
Working code:
import tkinter as tk
scrollbar = tk.Scrollbar(orient="horizontal")
e3 =tk.Entry(xscrollcommand=scrollbar.set)
e3.focus()
e3.pack(side="bottom",fill="x")
#e3.grid(row=10, column=7)
scrollbar.pack(fill="x")
scrollbar.config(command=e3.xview)
e3.config()
#EDIT: The last line (e3.config()) is unnecessary - it does nothing.

Related

Can't figure out where "{}" come from when updating a Label widget with a string from a list

I was trying to update the text of a label in tkinter with values stored in a list when I noticed that tkinter seemingly adds '{}' to the label text when the passed list element is empty.
import tkinter as tk
root = tk.Tk()
list1 = [["someText"], []]
tk.Label(root, text=list1).pack()
root.mainloop()
Can someone explain where the curly brackets come from and why they are added? Your help is appreciated.
Python's tkinter library is an interface for Tcl/Tk. As part of this interfacing python has to decide how to interpret the values that you pass it. If you pass it a value where str(value) is falsey then it replaces it with {}, as shown in this part of the source code.

I am not able to grab the date from the DateEntry using tkinter python

from tkinter import *
from tkinter.ttk import Combobox
from tkinter import messagebox
from tkinter import filedialog
from tkcalendar import *
root = Tk()
root.geometry('500x500')
def Date_func():
print(startdateEntry.get())
print(enddateEntry.get())
middle_frame=Frame(root,bg='#80c1ff',bd=10)
middle_frame.place(relx=0.5,rely=0.25,relwidth=0.75,relheight=0.2,anchor='n')
#Date and Time Entry boxes and buttons
StartHR=StringVar()
EndHR=StringVar()
StartMN=StringVar()
EndMN=StringVar()
StartDate=StringVar()
EndDate=StringVar()
StartHR.set('HH')
EndHR.set('HH')
StartMN.set('MM')
EndMN.set('MM')
starthourEntry=Entry(middle_frame,text=StartHR,width=5).grid(row=0,column=0)
endhourEntry=Entry(middle_frame,text=EndHR,width=5).grid(row=1,column=0)
startminuteEntry=Entry(middle_frame,text=StartMN,width=5).grid(row=0,column=1)
endminuteEntry=Entry(middle_frame,text=EndMN,width=5).grid(row=1,column=1)
startdateEntry = DateEntry(middle_frame,width=30,bg="darkblue",fg="white",year=2020).grid(row=0,column=2)
enddateEntry = DateEntry(middle_frame,width=30,bg="darkblue",fg="white",year=2020).grid(row=1,column=2,padx=30,pady=10)
selectStartDate_button=Button(middle_frame,text='StartDate',command=Date_func).grid(row=0,column=3)
selectEndDate_button=Button(middle_frame,text='EndDate',command=Date_func).grid(row=1,column=3)
root.mainloop()
I want to capture the date selected in the DateEntry but failed to do so,I am getting "
AttributeError: 'NoneType' object has no attribute 'get'"
I tried DateEntry.get_date as well.
The problem is that you are saying DateEntry(...).grid(...) in the same line, which returns None, it is not recommened to not do so and is a bad practice. So to fix it simply say it in two different lines, like:
startdateEntry = DateEntry(middle_frame,width=30,bg="darkblue",fg="white",year=2020)
startdateEntry.grid(row=0,column=2)
This is recommended to be done for all similar widgets on which you want to use attributes like get(), insert(), cget() and so on..
Then to get selection from this, I think you have to say startdateEntry.selection_get() instead of using the get() method.
Also as a side tip :-
Entry widget does not have text argument, unlike Button or Label, perhaps you meant textvariable?
Hope it cleared your doubts, do let me know if any doubts.
Cheers
I removed the .grid() to next line and get the work done.

ValueError: complex() arg is a malformed string

I have to take complex number as an input from Entry widget of tkinter and perform the conjugate operation on that complex number. I applied explicit conversion method but my code is not able to convert Entry widget string into complex number and showing error "ValueError: complex() arg is a malformed string" Can anyone help me?
Thank you in advance.
lbl_shh=Label(second_root,text="Enter parameter Shh",fg="red").grid(column=0,row=7,padx=20,pady=20)
e_shh = Entry(second_root)
lbl_svv=Label(second_root,text="Enter parameter Svv",fg="red").grid(column=0,row=8,padx=20,pady=20)
e_svv = Entry(second_root)
e_shh.grid(column=1,row=7)
e_svv.grid(column=1,row=8)
shh=e_shh.get()
svv=e_svv.get()
shh=shh.replace(" ","")
svv=svv.replace(" ","")
shh=complex(shh)
svv=complex(svv)
#shh=complex(''.join(shh.split()))
#svv=complex(''.join(svv.split()))
shhs=np.conjugate(shh)
svvs=np.conjugate(svv)
num= svv*svvs
dem=shh*shhs
f=np.power(num/dem, 0.25)
I have to print the value of f
I think you misunderstand how to properly get information within tkinter and probably Python in general.
You cannot just use .get() when your code is just initializing. It will always return an empty string unless you have some code that sets the value prior to get and at that point its just redundant to use get.
What you need to do is have some code like a button that will pull the value of your entry(s) after someone has added something to them.
Also I noticed in your example code you have second_root and this leads me to believe you are using 2 instances of Tk() in your code. If that is the case this may also be part of your problem. You should only ever have one instance of Tk() when coding in tkinter.
To ilistrate your problem Take this example:
I added some print statements, a function and a button to show what was actually being grabbed by get() or rather to show it is an empty string. If you do not have anything in the field by the time get() is executed.
And here is an example result from when you put a proper value that complex() can use.
See below example to get an idea of how get() works:
import tkinter as tk
root = tk.Tk()
entry = tk.Entry(root)
entry.pack()
def print_entry():
print(entry.get())
tk.Button(root, text='Print Entry', command=print_entry).pack()
root.mainloop()

PyQt Calculator- Not getting Result

In the following Program, the calculator gui is closing itself when i press "=" for getting the result. I think there is some problem with the eval function. if i remove the eval then i didn't encounter any problem with the program. Need help in figuring this out. Is there any other approach i can try besides this? Thanks in advance.
class Calci(QWidget):
def __init__(self, Parent=None):
super(Calci,self).__init__()
self.initgui()
def initgui(self):
self.list1=
["%","rt","pow","1/x","CE","C","BCK","/","7","8","9","*","4","5","6","-
","1","2","3","+","+-","0",".","="]
self.list2=[(i,j) for i in range(2,8) for j in range(0,4)]
self.button={}
self.data1=""
self.data2=0
self.lineedit=QLineEdit()
self.lineedit.setFocus()
grid=QGridLayout()
self.setLayout(grid)
self.font=QFont("Bookman Old Style",15,25)
self.lineedit.setFont(self.font)
for x,y in zip(self.list1,self.list2):
self.button[y]=QPushButton(x)
grid.addWidget(self.lineedit,0,0,2,4)
self.lineedit.setSizePolicy(QSizePolicy.Preferred,QSizePolicy.Expanding)
self.lineedit.setMinimumHeight(70)
grid.addWidget(self.button[y],*y)
self.button[y].setSizePolicy(QSizePolicy.Preferred,QSizePolicy.Expanding)
self.button[y].setMinimumHeight(70)
self.lineedit.setAlignment(Qt.AlignRight)
self.button[y].clicked.connect(lambda state,x=x: self.click(x))
def click(self,n):
if (n=="="):
data1=self.lineedit.text()
self.lineedit.clear()
self.lineedit.insert(eval(data1))
else:
self.lineedit.insert(n)
app=QApplication(sys.argv)
calci=Calci()
calci.show()
app.exec_()
It's most likely failing because of a TypeError when you try to set the line edit text to an integer value. I also don't think self.lineedit.insert() is the method that you want to use. This will add the calculation after the line after the text already entered.
try:
self.lineedit.setText(str(eval(data1)))
This will clear the line edit and set the text to the calculated value.
But:
Please don't use eval like this. This is a very dangerous practice as any python code entered in the text edit will be run.

tkinter scrolledtext.insert and label.configuration point of execution

I have the following python36 code wich is checking values, and then set a tkinter label that the entry values has been accepted, to then execute another threaded function. Before and after the function as well as inside the function I would like to display something in a tkinter scrolledtext box. Like it where a console/shell.
All the label.configure() as well as scrolledtext.insert() are only getting displayed all at the same time after everything has been run.
I am not able to use the scrolledtext.insert() inside the threaded function (fundamentals question, could I use it inside a function of an imported module?)
I would like to have the execution time of these functions like if I would use the print() function. So execute it as soon as the script went over it.
It would be nice if you could explain to me why this is not executed immideately since I am currently learning python or point me to the appropriate reference.
elif str(x2) == 'None' and str(x3) == 'None':
E2T = 'accepted'
E2L.configure(text=E2T, fg="green")
E2L.grid(row=5, column=2)
E3T = 'accepted'
E3L.configure(text=E3T, fg="green")
E3L.grid(row=6, column=2)
# Start scanning process
scrolledtext.insert(tkinter.INSERT, 'Start scanning....\n' )
print('testprint')
portlist = scan(E1.get(),E2.get(),E3.get())
# try work with returned value and display as in a console
print(portlist)
print('testprint')
scrolledtext.insert(tkinter.INSERT, 'Following Ports are open\n' )
scrolledtext.insert(tkinter.INSERT, str(portlist))
You can do scrolledtext.update_idletasks() after a scrolledtext.insert(), it will refresh the widget and your text will pop.
see that comment for more.
Hope it help!

Resources