How do I use grid system on entry widget affected by .get() - python

my code:
import tkinter as tk
from tkinter import *
truth = ""
us = ""
uss = ""
root = Tk()
s = Label(root, text = ".")
s.grid(row = 1, column = 1)
wel = Label(root, text = "whats your email")
wel.grid(row = 1, column = 5)
inp = Entry(root).get()
inp.grid(row = 3, column = 5)
def callback():
us = inp[:inp.index("#")]
uss = inp[inp.index("#")+1:]
truth =us, " is username, and ", uss,"is domain"
print(truth)
sub = Button(root, text = "submit", command = callback)
sub.grid(row = 5, column = 5)
final = Label(root, textvariable = truth)
final.grid(row = 5, column = 6)
root.mainloop()
Then I get an error message:
'str' object has no attribute 'grid'
How do I use grid system on entry widget affected by .get()?

Related

Tkinkter no accepting country code properly

I am making this college project using tkinter GUI and pywhatkit. It's showing Country code missing even if I put it (with symbol).
My code:
from tkinter import *
import pywhatkit as kit
import tkinter.messagebox as tmsg
root = Tk()
root.title("Whatsapp Auto Message Scheduler")
p_num = str(Label(root, text = 'Enter the phone number(with Country code):').grid(row = 0))
msg = Label(root, text = 'The message:').grid(row = 1)
h = Label(root, text = 'Timing(hour, as per 24hr clock):').grid(row = 2)
m = Label(root,text = 'Timing(minute):').grid(row = 3)
e1 = Entry(root)
e2 = Entry(root)
e3 = Entry(root)
e4 = Entry(root)
e1.grid(row = 0, column = 1)
e2.grid(row = 1, column = 1)
e3.grid(row = 2, column = 1)
e4.grid(row = 3, column = 1)
def pywhatkit():
p = kit.sendwhatmsg(p_num, msg, h, m)
tmsg.showinfo("Whatsapp Auto Mesage Scheduler","Response Submitted!")
return p
B = Button(root, text = 'Submit', command = pywhatkit)
B.grid(row=4,column=1)
root.mainloop()
The error it's showing:
There are a couple of problems. Have you actually checked to see what p_num = str(Label(...)) is? Let's try it in the shell:
>>> import tkinter
>>> str(tkinter.Label(text="Hello world").grid(row=0))
'None'
>>>
I won't get really into it, but the correct way of getting a user's entry is by invoking .get() on the associated entry widget:
Label(root, text = 'Enter the phone number(with Country code):').grid(row = 0)
Label(root, text = 'The message:').grid(row = 1)
Label(root, text = 'Timing(hour, as per 24hr clock):').grid(row = 2)
Label(root, text = 'Timing(minute):').grid(row = 3)
phone_number_entry = Entry(root)
message_entry = Entry(root)
hour_entry = Entry(root)
minute_entry = Entry(root)
phone_number_entry.grid(row = 0, column = 1)
message_entry.grid(row = 1, column = 1)
hour_entry.grid(row = 2, column = 1)
minute_entry.grid(row = 3, column = 1)
def pywhatkit():
phone_number = phone_number_entry.get()
message = message_entry.get()
hour = int(hour_entry.get())
minute = int(minute_entry.get())
kit.sendwhatmsg(phone_number, message, hour, minute)
tmsg.showinfo("Whatsapp Auto Mesage Scheduler","Response Submitted!")

how to show/hide widget in tkinter without moving other widget

I'm using grid_remove() and grid() command to hide/show the widget but the result is the other widget is move out of the original position.
How to hide/show the widget without moving widget
Example:
from tkinter import *
from tkinter import ttk
GUI = Tk()
GUI.title("myTest")
GUI.geometry("700x700")
Nameget = StringVar()
Priceget = StringVar()
Quantityget = StringVar()
Unitget = StringVar()
Partnumget = StringVar()
L_Partnum = ttk.Label(GUI, text = 'Part number')
L_Partnum.grid(row = 0, column = 0)
L_namme = ttk.Label(GUI, text = 'Name')
L_namme.grid(row = 0, column = 1)
L_quan = ttk.Label(GUI, text = 'Quantity')
L_quan.grid(row = 1, column = 2)
L_quan.grid_remove()
L_price = ttk.Label(GUI, text = 'Price')
L_price.grid(row = 3, column = 3)
E_partnum = ttk.Entry(GUI, textvariable = Partnumget)
E_partnum.grid(row = 1, column = 0)
E_namme = ttk.Entry(GUI,textvariable = Nameget)
E_namme.grid(row = 1, column = 1)
E_unit = ttk.Entry(GUI,textvariable = Unitget)
E_quan = ttk.Entry(GUI,textvariable = Quantityget)
E_quan.grid(row = 2, column = 2)
E_quan.grid_remove()
E_price = ttk.Entry(GUI,textvariable = Priceget)
E_price.grid(row = 4, column = 3)
I_check_vat = IntVar()
def d_check_vat_1():
E_partnum.focus()
if I_check_vat.get() == 1:
L_quan.grid()
E_quan.grid()
elif I_check_vat.get() == 0:
L_quan.grid_remove()
E_quan.grid_remove()
C_CHECK_VAT = ttk.Checkbutton(GUI, text="click here to see the result", variable=I_check_vat, command=d_check_vat_1)
C_CHECK_VAT.grid(row = 5, column = 0)
GUI.mainloop()
Before clicking:
After clicking:
image with the expected output:
The problem is grid() does not take up empty space by default, it gives the last empty row/col to the widget(if previous rows before it are empty).
So what you can do is, set minimum space for your column and row so that those space will remain empty, so change your function to:
def d_check_vat_1():
E_partnum.focus()
if I_check_vat.get():
L_quan.grid(row=2, column=2)
E_quan.grid(row=3, column=2)
width = E_quan.winfo_reqwidth() # Get widget width
height = L_quan.winfo_reqheight() # Get widget height
GUI.rowconfigure(2,minsize=height) # Now apply the values
GUI.rowconfigure(3,minsize=height)
GUI.columnconfigure(2,minsize=width)
else:
L_quan.grid_remove()
E_quan.grid_remove()
Now its dynamic as well, it takes the width of widget and applies that as the minsize of that row so that row will have that empty space.

Get variable value created inside a function

I want to get a variable from a function to use this variable in another function.
Simple example:
from tkinter import *
def test():
intro = "I am "
name = "Phil"
text = intro + name
def printresult():
print(text)
root = Tk()
root.title("Test")
testbutton = Button(root, text="Test", command = test)
printbutton = Button(root, text="print Test", command = printresult)
testbutton.grid(row = 1, column = 0)
printbutton.grid(row = 1, column = 1)
mainloop()
If I press the testbutton and afterwards the printbutton, then I get the error "name 'text' is not defined".
So how am I able to get the text variable from the def test() to use it in the def printresult()?
You need to save the value somewhere well known:
from tkinter import *
def test():
intro = "I am "
name = "Phil"
text = intro + name
test.text = text # save the variable on the function itself
def printresult():
print(test.text)
root = Tk()
root.title("Test")
testbutton = Button(root, text="Test", command = test)
printbutton = Button(root, text="print Test", command = printresult)
testbutton.grid(row = 1, column = 0)
printbutton.grid(row = 1, column = 1)
mainloop()
Since you are using tkinter, I'd use a StringVar to store the result. Using a string var makes it easy for other tkinter widgets to use the value.
from tkinter import *
def test():
intro = "I am "
name = "Phil"
text.set(intro + name)
def printresult():
print(text.get())
root = Tk()
root.title("Test")
text = StringVar()
testbutton = Button(root, text="Test", command = test)
printbutton = Button(root, text="print Test", command = printresult)
testbutton.grid(row = 1, column = 0)
printbutton.grid(row = 1, column = 1)
mainloop()

How to set an output from button (command = def()), as an input to Combobox values =

I'm trying to set an input to Combobox(values = output) from other
function (which connected to Button(command = some_function)
from tkinter import ttk
from tkinter import filedialog
from tkinter import *
def select():
global sel
a = ['101','102','103','104','105']
b = ['201','202','203','204','205']
sel = []
#label.configure(text = " Fleet" + fleet.get())
choosed = fleet.curselection()
for i in choosed:
selection = fleet.get(i)
print ("selected " + " " + selection)
if selection == 'B':
sel = b
else: sel = a
#print (sel)
return sel
root =Tk()
fleet = Listbox(root, width = 10, height = 2)
fleet.insert(1, 'B')
fleet.insert(2, 'A')
fleet.grid(column = 1, row = 0)
label = ttk.Label(root, text = "Please choose the fleet")
label.grid (column = 0, row = 0)
button1 = ttk.Button(root, text = 'Select', command = select)
button1.grid(column = 0, row = 1)
a = ['101','102','103','104','105']
b = ['201','202','203','204','205']
combo_tool_num = ttk.Combobox(root, width = 10, values = sel)
I would like to set an select() output sel, as an input for: combo_tool_num values = sel.
Thank you!
To set initial value in combobox use 'set()'
Use syntax as,
combo_tool_num = ttk.Combobox(root, width = 10, values = sel)
combo_tool_num.set('Select')

How to display output of print() in GUI python

I am new in creating GUI. I am doing it in Python with Tkinter. In my program I calculate following characteristics
def my_myfunction():
my code ...
print("Centroid:", centroid_x, centroid_y)
print("Area:", area)
print("Angle:", angle)
I would like to ask for any help/tips how to display those values in GUI window or how to save them in .txt file so that I can call them in my GUI
Thanks in advance
Tkinter is easy and an easy way to do a GUI, but sometimes it can be frustrating. But you should have read the docs before.
However, you can do in this way.
from tkinter import *
yourData = "My text here"
root = Tk()
frame = Frame(root, width=100, height=100)
frame.pack()
lab = Label(frame,text=yourData)
lab.pack()
root.mainloop()
There are several ways to display the results of any operation in tkiner.
You can use Label, Entry, Text, or even pop up messages boxes. There are some other options but these will probably be what you are looking for.
Take a look at the below example.
I have a simple adding program that will take 2 numbers and add them together. It will display the results in each kind of field you can use as an output in tkinter.
import tkinter as tk
from tkinter import messagebox
class App(tk.Frame):
def __init__(self, master):
self.master = master
lbl1 = tk.Label(self.master, text = "Enter 2 numbers to be added \ntogether and click submit")
lbl1.grid(row = 0, column = 0, columnspan = 3)
self.entry1 = tk.Entry(self.master, width = 5)
self.entry1.grid(row = 1, column = 0)
self.lbl2 = tk.Label(self.master, text = "+")
self.lbl2.grid(row = 1, column = 1)
self.entry2 = tk.Entry(self.master, width = 5)
self.entry2.grid(row = 1, column = 2)
btn1 = tk.Button(self.master, text = "Submit", command = self.add_numbers)
btn1.grid(row = 2, column = 1)
self.lbl3 = tk.Label(self.master, text = "Sum = ")
self.lbl3.grid(row = 3, column = 1)
self.entry3 = tk.Entry(self.master, width = 10)
self.entry3.grid(row = 4, column = 1)
self.text1 = tk.Text(self.master, height = 1, width = 10)
self.text1.grid(row = 5, column = 1)
def add_numbers(self):
x = self.entry1.get()
y = self.entry2.get()
if x != "" and y != "":
sumxy = int(x) + int(y)
self.lbl3.config(text = "Sum = {}".format(sumxy))
self.entry3.delete(0, "end")
self.entry3.insert(0, sumxy)
self.text1.delete(1.0, "end")
self.text1.insert(1.0, sumxy)
messagebox.showinfo("Sum of {} and {}".format(x,y),
"Sum of {} and {} = {}".format(x, y, sumxy))
if __name__ == "__main__":
root = tk.Tk()
myapp = App(root)
root.mainloop()

Categories