Entry boxes don't give results - python

from tkinter import *
import os
os.system ("clear")
root = Tk()
root.title('Test')
root.geometry('1000x600')
def submit():
submit_text = Label(text=(eq))
submit_text.pack()
label = Label(root, text='Enter the first mounth')
label.pack()
textbox3 = Entry(root, width=30)
textbox3.pack()
textbox2 = Entry(root, width=30)
textbox2.pack()
textbox1 = Entry(root, width=30)
textbox1.pack()
eq = textbox1.get() + textbox2.get() + textbox3.get()
button = Button(root, text="submit", command=submit)
button.pack()
root.mainloop()
I want the code to open up a window and I'm going to put 3 numbers and I want the code to add the 3 numbers and show me the result when II press the button, but it does nothing.

Related

If-Else not working in Tkinter under functions please solve this question :

from tkinter import *
import tkinter.messagebox as tmsg
root = Tk()
def myaccount():
Label(root, text="\n\nEnter Your Account Number : ").pack(anchor="nw")
global en
en = Entry(font=" helectiva 11")
en.pack(anchor="nw")
def detail():
if en=="91xxxxxxxx":
Label(text="Hello").pack()
b = Button(root, text="View my Account Details",borderwidth=10,command=detail)
b.pack(anchor="nw")
txt = StringVar()
txt.set("——Welcome To Banking System Application——")
txt1 = StringVar()
txt1.set("*****************************************************")
txt2 = StringVar()
txt2.set("\n\nChoose from the Options :")
txt3 = StringVar()
txt3.set("—————————————————————————————")
Label(root, textvariable = txt).pack()
Label(root, textvariable = txt1).pack()
Label(root, textvariable = txt2).pack()
Label(root, textvariable= txt3).pack()
Button(root, text="View My Account",borderwidth=10,command=myaccount).pack(anchor="nw",ipadx=32)
Button(root, text="New Account",borderwidth=10).pack(anchor="nw",ipadx=72)
Button(root, text="Make a Transactiom",borderwidth=10).pack(anchor="nw")
Button(root, text="Exit",borderwidth=10,command=quit).pack(anchor="nw",ipadx=170)
root.mainloop()
Please help me with this. The if clause in my detail function is not working. I tried to solve it for 5 hours straight but couldn't find the correct reason. But if you find the reason please give the answer, I will be very thankful to that person.
An entry object cannot be a string. Instead, you must get the text inside the entry object:
if en.get() == '91xxxxxxxx'
Label(text="Hello").pack()
Final code:
from tkinter import *
import tkinter.messagebox as tmsg
root = Tk()
def myaccount():
Label(root, text="\n\nEnter Your Account Number : ").pack(anchor="nw")
global en
en = Entry(font=" helectiva 11")
en.pack(anchor="nw")
def detail():
if en.get() == '91xxxxxxxx'
Label(text="Hello").pack()
b = Button(root, text="View my Account Details",borderwidth=10,command=detail)
b.pack(anchor="nw")
txt = StringVar()
txt.set("——Welcome To Banking System Application——")
txt1 = StringVar()
txt1.set("*****************************************************")
txt2 = StringVar()
txt2.set("\n\nChoose from the Options :")
txt3 = StringVar()
txt3.set("—————————————————————————————")
Label(root, textvariable = txt).pack()
Label(root, textvariable = txt1).pack()
Label(root, textvariable = txt2).pack()
Label(root, textvariable= txt3).pack()
Button(root, text="View My Account",borderwidth=10,command=myaccount).pack(anchor="nw",ipadx=32)
Button(root, text="New Account",borderwidth=10).pack(anchor="nw",ipadx=72)
Button(root, text="Make a Transactiom",borderwidth=10).pack(anchor="nw")
Button(root, text="Exit",borderwidth=10,command=quit).pack(anchor="nw",ipadx=170)
root.mainloop()

I want to take user input and output it inside GUI

I want to take user input and output it inside GUI ...
my code
from tkinter import *
root = Tk()
root.geometry("644x344")
def printSomething():
label = Label(root, text="???")
label.grid(row=1, column=2)
ok=Label(root, text="Type your name").grid(row=2,column=1)
entryvalue = StringVar()
entry= Entry(root, textvariable=entryvalue)
entry.grid(row=2, column=2)
button = Button(root, text="Print Me", command=printSomething)
button.grid(row=3, column=2)
root.mainloop()
To get the text from an input box, use inputbox.get(). Also, don't set the ok variable to Label(root, text="Type your name").grid(row=2,column=1). This will be set as NoneType, so do
ok = Label(root, text="Type your name").grid(row=2,column=1)
ok.grid(row=2, column=1)
Here is your code:
from tkinter import *
root = Tk()
root.geometry("644x344")
def printSomething():
label = Label(root, text=entry.get())
label.grid(row=1, column=2)
ok=Label(root, text="Type your name")
ok.grid(row=2,column=1)
entryvalue = StringVar()
entry= Entry(root, textvariable=entryvalue)
entry.grid(row=2, column=2)
button = Button(root, text="Print Me", command=printSomething)
button.grid(row=3, column=2)
root.mainloop()
First thing, In order to accept and display the output on the screen you have to use either Label widget or Canvas Text. Since your code is not updating the Label widget thus I am here doing what you want to do.
First, create a Label widget in the main window,
Get the user input by using.get() method,
print and display the user input.
from tkinter import *
root = Tk()
root.geometry("644x344")
def printSomething():
label.config(text=entry.get())
ok=Label(root, text="Type your name")
ok.grid(row=2,column=1)
entryvalue = StringVar()
entry= Entry(root, textvariable=entryvalue)
entry.grid(row=2, column=2)
button = Button(root, text="Print Me", command=printSomething)
button.grid(row=3, column=2)
#Create a Label to print the Name
label= Label(root, text="", font= ('Helvetica 14 bold'), foreground= "red3")
label.grid(row=1, column=2)
root.mainloop()

Why does this not add?

I've looked other questions and have tried to fix this but I am really struggling with having my code print back the answer.
from tkinter import *
root = Tk()
label1 = Label(root, text="Number 1")
label2 = Label(root, text="Number 2")
labelplus = Label(root, text="+")
label1.grid(row=0, sticky=E)
label2.grid(row=3, sticky=E)
labelplus.grid(row=2, column=1)
entry1_var= StringVar()
entry2_var= StringVar()
entry1 = Entry(root, textvariable= entry1_var)
entry2 = Entry(root, textvariable= entry2_var)
entry1.grid(row=0, column=1)
entry2.grid(row=3, column=1)
first = (entry1_var.get()
second =(entry2_var.get()
def additionStuff(event):
totalNumbers = (first + second)
print(totalNumbers)
button1 = Button(root, text="Add Numbers")
button1.bind("<Button-1>", additionStuff)
button1.grid(row=4, column=1)
root.mainloop()
Why does my function now print back the answer?
You need to call StringVar.get inside of additionStuff:
def additionStuff(event):
first = entry1_var.get()
second = entry2_var.get()
totalNumbers = (float(first) + float(second))
print(totalNumbers)
Otherwise you're getting the value of first and second before the user has had opportunity to enter anything.

Displaying prints in GUI

I made this GUI:
And after I click 'Run', my main program starts.
In my main program, I have a lot of prints that I want to display in the GUI, but have no idea how.
I saw some examples in google but it was really hard to understand and how to convert it to my needs, so: How to make a window with all the prints after I click the Run button ?
code:
from tkinter import *
from main import *
root = Tk()
root.configure(background="orange")
root.wm_title("Python Project")
label_1 = Label(root, text="Project Name",bg="orange",fg="black")
label_2 = Label(root, text="Site URL Link",bg="orange",fg="black")
entry_1 = Entry(root)
entry_2 = Entry(root)
label_1.grid(row=0,sticky=W)
label_2.grid(row=3333,sticky=W)
entry_1.grid(row=0, column=1, padx=50, ipadx=100)
entry_2.grid(row=3333, column=1, ipadx=100)
def callback():
a1 = entry_1.get()
a2 = entry_2.get()
mmm(a1,a2) # main program
button1 = Button(root,text="Run",command=callback)
button2 = Button(root,text="Quit",command=root.quit)
button1.grid(row=3334, ipadx=15, padx=50, column=1)
button2.grid(row=3335, column=1, ipadx=15, padx=50)
root.mainloop()

how to save text into a txt file using python 2.7 using tk

Im using tkinter and I am trying to get text that's entered by a user to be saved into a existing txt file when they click on save, any ideas.
from Tkinter import *
root = Tk()
w1 = Label(root, text="Username")
w1.pack()
e = Entry(root)
e.pack()
w2 = Label(root, text="Password")
w2.pack()
e1 = Entry(root)
e1.pack()
toolbar = Frame(root)
b = Button(toolbar, text="save", width=9)
b.pack(side=LEFT, padx=2, pady=2)
toolbar.pack(side=TOP, fill=X)
mainloop()
The simplest way is to create a function that is called when the save button is clicked. Put it near the top of your script and then set it as the button's command.
def save():
text = e.get() + " " + e1.get() + "\n"
with open("text.txt", "a") as f:
f.write(text)
# Snip
b = Button(toolbar, text="save", width=9, command=save)

Categories

Resources