my pycharm console is blank while printing tkinter code - python

this code should have given the input data i the console , but my pycharm console is blank without printing the input data also, it doesnt show any kind of error . see the code and help me solve this
i am a beiginner in python and this pycharm ide .help me
from tkinter import *
win = Tk()
win.geometry("400x289")
Label(win, text=" bhandari hotel ", font=" aeiral11 13 bold").grid(row=0, column=2)
name = Label(win, text="name").grid(row=1, column=0)
phone = Label(win, text="phone").grid(row=2, column=0)
gender = Label(win, text="gender").grid(row=3, column=0)
emergency = Label(win, text="emergency").grid(row=4, column=0)
payment = Label(win, text="payment").grid(row=5, column=0)
namevar = StringVar()
phonevar = StringVar()
gendervar = StringVar()
emergencyvar = StringVar()
paymentvar = StringVar()
check = IntVar()
namee = Entry(win, textvariable="namevar")
namee.grid(row=1, column=3)
phonee = Entry(win, textvariable="phonevar")
phonee.grid(row=2, column=3)
gendee = Entry(win, textvariable="gendervar")
gendee.grid(row=3, column=3)
emergencye = Entry(win, textvariable="emergencyvar")
emergencye.grid(row=4, column=3)
paymente = Entry(win, textvariable="paymentvar")
paymente.grid(row=5, column=3)
foodservice = Checkbutton(win, text='do you agree?', variable=check).grid(row=6, column=3)
def amazing():
print("hello"+ str(namevar.get()))
Button(text="submit", bg='gold', command=amazing).grid()
win.mainloop()
please help me solve this .

namee = Entry(win, textvariable=namevar)
namee.grid(row=1, column=3)
phonee = Entry(win, textvariable=phonevar)
phonee.grid(row=2, column=3)
gendee = Entry(win, textvariable=gendervar)
gendee.grid(row=3, column=3)
emergencye = Entry(win, textvariable=emergencyvar)
emergencye.grid(row=4, column=3)
paymente = Entry(win, textvariable=paymentvar)
paymente.grid(row=5, column=3)
don't assign the variable in strings. here's the change.
It's just a suggestion
use :
a=Entry(..)
a.grid(..)
instead of
a=Entry(...).grid(...)
whole modified code:
from tkinter import *
win = Tk()
win.geometry("400x289")
Label(win, text=" bhandari hotel ", font=" aeiral11 13 bold").grid(row=0, column=2)
name = Label(win, text="name").grid(row=1, column=0)
phone = Label(win, text="phone").grid(row=2, column=0)
gender = Label(win, text="gender").grid(row=3, column=0)
emergency = Label(win, text="emergency").grid(row=4, column=0)
payment = Label(win, text="payment").grid(row=5, column=0)
namevar = StringVar()
phonevar = StringVar()
gendervar = StringVar()
emergencyvar = StringVar()
paymentvar = StringVar()
check = IntVar()
namee = Entry(win, textvariable=namevar)
namee.grid(row=1, column=3)
phonee = Entry(win, textvariable=phonevar)
phonee.grid(row=2, column=3)
gendee = Entry(win, textvariable=gendervar)
gendee.grid(row=3, column=3)
emergencye = Entry(win, textvariable=emergencyvar)
emergencye.grid(row=4, column=3)
paymente = Entry(win, textvariable=paymentvar)
paymente.grid(row=5, column=3)
foodservice = Checkbutton(win, text='do you agree?', variable=check).grid(row=6, column=3)
def amazing():
print("hello "+ str(namevar.get()))
Button(text="submit", bg='gold', command=amazing).grid()
win.mainloop()

Related

How do I print the random outcome of an entry input in a list?

Code
list = [ee1.get(), ee2.get(), ee3.get(), ee4.get(), ee5.get(), ee6.get(), ee7.get(), ee8.get(), ee9.get(), ee10.get()]
context = "What does", random.choice(list),"mean?"
labelquestion = Label(window, text = context, font = "Serif 10 bold")
Question
When I run this, it outputs "{What does} {} mean?"
How can I fix this so that it outputs "What does"-random entry from list-"this mean?"
(The things in the list are entries)
Below is an example based on your code and information in the comment:
import tkinter as tk
import random
window = tk.Tk()
ee1 = tk.Entry(window)
ee1.grid(row=0, column=0)
ee2 = tk.Entry(window)
ee2.grid(row=0, column=1)
ee3 = tk.Entry(window)
ee3.grid(row=0, column=2)
ee4 = tk.Entry(window)
ee4.grid(row=0, column=3)
ee5 = tk.Entry(window)
ee5.grid(row=0, column=4)
ee6 = tk.Entry(window)
ee6.grid(row=1, column=0)
ee7 = tk.Entry(window)
ee7.grid(row=1, column=1)
ee8 = tk.Entry(window)
ee8.grid(row=1, column=2)
ee9 = tk.Entry(window)
ee9.grid(row=1, column=3)
ee10 = tk.Entry(window)
ee10.grid(row=1, column=4)
def submit():
words = [ee1.get(), ee2.get(), ee3.get(), ee4.get(), ee5.get(),
ee6.get(), ee7.get(), ee8.get(), ee9.get(), ee10.get()]
context = f"What does '{random.choice(words)}' mean?"
labelquestion.config(text=context)
tk.Button(window, text="Submit", command=submit).grid(row=2, column=0, sticky='w')
labelquestion = tk.Label(window, font="Serif 10 bold")
labelquestion.grid(row=2, column=1, columnspan=4)
window.mainloop()

Is there a way for me to use a button to print entry's in tkinter

Hi im fairly new to tkinter and python, so i'm just wondering if there is any way for me to use my 'add' button to print 'name' 'email' and 'password' entry's underneath my 'history' Label. The purpose is to help you remember your accounts. Although i don't need the function to keep this data when closed.
Any help would be much appreciated,
Thanks.
Here is my code:
from tkinter import *
import tkinter as tk
root = Tk()
root.title("Login")
username = "Sam"
password = "Sam"
#username entry
username_entry = Entry(root, borderwidth=10)
#password entry
password_entry = Entry(root, show='*', borderwidth=10)
username_entry.grid(row=1,column=2)
password_entry.grid(row=2,column=2)
myLabel1 = Label(root,text="Username")
myLabel2 = Label(root,text="Password")
myLabel1.grid(row=1, column=0)
myLabel2.grid(row=2, column=0)
def trylogin():
if username == username_entry.get() and password == password_entry.get():
print("Correct")
createNewWindow()
else:
print("Wrong")
def createNewWindow():
newWindow = tk.Toplevel(root)
newWindow.title("Accounts")
#ENTRY'S
e1 = Entry(newWindow, width=50)
e2 = Entry(newWindow, width=50)
e3 = Entry(newWindow, width=50)
e1.grid(row=1, column=2)
e2.grid(row=2, column=2)
e3.grid(row=3, column=2)
#LABELS
myLabel1 = Label(newWindow, text="Name")
myLabel2 = Label(newWindow, text="EMAIL:")
myLabel3 = Label(newWindow, text="PASSWORD:")
myLabel4 = Label(newWindow, text="History:")
myLabel1.grid(row=1, column=0)
myLabel2.grid(row=2, column=0)
myLabel3.grid(row=3, column=0)
myLabel4.grid(row=4, column=1)
def addToWindow():
myLabel5 = Label(newWindow, text="Name: " + e1.get())
myLabel6 = Label(newWindow, text="Email: " + e2.get())
myLabel7 = Label(newWindow, text="Password: " + e3.get())
myLabel5.grid(row=7, column=1)
myLabel6.grid(row=8, column=1)
myLabel7.grid(row=9, column=1)
button = Button(newWindow, text="Add", command = addToWindow)
button.grid(row=4, column=2)
button = Button(root, text="check", command = trylogin)
button.grid(row=3, column=2)
root.mainloop()
app.mainloop()
I added a function within your createNewWindow function to write the values of the input fields below the history heading. This could be prettier, but you have the values listed.
Code below. Drop this in at line 53 of your script and call the command in your Button function for your add button.
from tkinter import *
import tkinter as tk
root = Tk()
root.title("Login")
username = "username"
password = "password"
#username entry
username_entry = Entry(root, borderwidth=10)
#password entry
password_entry = Entry(root, show='*', borderwidth=10)
username_entry.grid(row=1,column=2)
password_entry.grid(row=2,column=2)
myLabel1 = Label(root,text="Username")
myLabel2 = Label(root,text="Password")
myLabel1.grid(row=1, column=0)
myLabel2.grid(row=2, column=0)
def trylogin():
if username == username_entry.get() and password == password_entry.get():
print("Correct")
createNewWindow()
else:
print("Wrong")
def createNewWindow():
newWindow = tk.Toplevel(root)
newWindow.title("Accounts")
#ENTRY'S
e1 = Entry(newWindow, width=50)
e2 = Entry(newWindow, width=50)
e3 = Entry(newWindow, width=50)
e1.grid(row=1, column=2)
e2.grid(row=2, column=2)
e3.grid(row=3, column=2)
#LABELS
myLabel1 = Label(newWindow, text="Name")
myLabel2 = Label(newWindow, text="EMAIL:")
myLabel3 = Label(newWindow, text="PASSWORD:")
myLabel4 = Label(newWindow, text="History:")
myLabel1.grid(row=1, column=0)
myLabel2.grid(row=2, column=0)
myLabel3.grid(row=3, column=0)
myLabel4.grid(row=5, column=1)
def addToWindow():
myLabel5 = Label(newWindow, text="Name: " + e1.get())
myLabel6 = Label(newWindow, text="Email: " + e2.get())
myLabel7 = Label(newWindow, text="Password: " + e3.get())
myLabel5.grid(row=7, column=1)
myLabel6.grid(row=8, column=1)
myLabel7.grid(row=9, column=1)
button = Button(newWindow, text="Add", command = addToWindow)
button.grid(row=4, column=2)
button = Button(root, text="check", command = trylogin)
button.grid(row=3, column=2)
root.mainloop()
#app.mainloop()
Minor Fix for your req, move the function inside the Newwindow function
from tkinter import *
import tkinter as tk
root = Tk()
root.title("Login")
username = "Sam"
password = "Sam"
#username entry
username_entry = Entry(root, borderwidth=10)
#password entry
password_entry = Entry(root, show='*', borderwidth=10)
username_entry.grid(row=1,column=2)
password_entry.grid(row=2,column=2)
myLabel1 = Label(root,text="Username")
myLabel2 = Label(root,text="Password")
myLabel1.grid(row=1, column=0)
myLabel2.grid(row=2, column=0)
def trylogin():
if username == username_entry.get() and password == password_entry.get():
print("Correct")
createNewWindow()
else:
print("Wrong")
def createNewWindow():
newWindow = tk.Toplevel(root)
newWindow.title("Accounts")
#ENTRY'S
e1 = Entry(newWindow, width=50)
e2 = Entry(newWindow, width=50)
e3 = Entry(newWindow, width=50)
e1.grid(row=1, column=2)
e2.grid(row=2, column=2)
e3.grid(row=3, column=2)
#LABELS
myLabel1 = Label(newWindow, text="Name")
myLabel2 = Label(newWindow, text="EMAIL:")
myLabel3 = Label(newWindow, text="PASSWORD:")
myLabel4 = Label(newWindow, text="History:")
myLabel1.grid(row=1, column=0)
myLabel2.grid(row=2, column=0)
myLabel3.grid(row=3, column=0)
myLabel4.grid(row=4, column=1)
def addToWindow():
myLabel5 = Label(newWindow, text="Name: " + e1.get())
myLabel6 = Label(newWindow, text="Email: " + e2.get())
myLabel7 = Label(newWindow, text="Password: " + e3.get())
myLabel5.grid(row=7, column=1)
myLabel6.grid(row=8, column=1)
myLabel7.grid(row=9, column=1)
button = Button(newWindow, text="Add", command = addToWindow)
button.grid(row=4, column=2)
button = Button(root, text="check", command = trylogin)
button.grid(row=3, column=2)
root.mainloop()
app.mainloop()

Tkinter - Subprocess to create user

I have written the following to allow users to be added,the script runs fine with no errros but no user account is created.
from tkinter import *
import subprocess
def add():
subprocess.call(['adduser', '-N', '-g', group.get(), '-c', fname.get(), '-d', '/home/oucu.get()'])
root = Tk()
oucu = StringVar()
e2 = Entry(root, textvariable=oucu).grid(row=0, column=1)
fname = StringVar()
e3 = Entry(root, textvariable=fname).grid(row=1, column=1)
sname = StringVar()
e4 = Entry(root, textvariable=sname).grid(row=2, column=1)
group = StringVar()
e6 = Entry(root, textvariable=group).grid(row=3, column=1)
b1 = Button(root, text='Next', command=add).grid(row=4, column=1)
Label(root, text="Users Oucu").grid(row=0, column=0, sticky=W)
Label(root, text="First Name").grid(row=1, column=0, sticky=W)
Label(root, text="Last Name").grid(row=2, column=0, sticky=W)
Label(root, text="Group").grid(row=3, column=0, sticky=W)
root.mainloop()

How to align label, entry in tkinter

I am really frustrated, trying to align a label and entry buttons in tkinter.
I wanted to create a GUI like below
The above page developed using page tool. I written a code to get the same kind of page, but it has lot of misalignment.
from Tkinter import *
root = Tk()
root.title("Nokia Performance")
root.geometry("583x591+468+158")
root.title("NOKIA _ANSI Performance")
root.configure(borderwidth="1")
root.configure(relief="sunken")
root.configure(background="#dbd8d7")
root.configure(cursor="arrow")
root.configure(highlightbackground="#d9d9d9")
root.configure(highlightcolor="black")
Label1 = Label(root)
_img1 = PhotoImage(file="C:\\Users\\vkandhav\\Desktop\\PY_IMAGE\\NOKIA.gif")
Label1.configure(image=_img1)
Label1.configure(text='''Label''')
Label1.pack()
Label2 = Label(root)
Label2.configure(text='''Enter the platform :''')
Label2.pack(side=LEFT)
Entry2 = Entry(root)
Entry2.pack(side = RIGHT)
Label3 = Label(root)
Label3.configure(text='''Device IP Address :''')
Label3.pack()
Entry5 = Entry(root)
Entry5.pack()
Label5 = Label(root)
Label5.configure(text='''Username :''')
Label5.pack()
Label6 = Label(root)
Label6.configure(text='''Label''')
Label6.pack()
Label7 = Label(root)
Label7.configure(text='''Craft IP :''')
Label7.pack()
Label8 = Label(root)
Label8.configure(text='''GICCI IP :''')
Label8.pack()
Label9 = Label(root)
Label9.configure(text='''Password :''')
Label9.pack()
Label10 = Label(root)
Label10.configure(text='''STC File Location''')
Label10.pack()
Label11 = Label(root)
Label11.configure(text='''STC Port :''')
Label1.pack()
Label12 = Label(root)
Label12.configure(text='''STC IP :''')
Label2.pack()
Entry6 = Entry(root)
Entry6.configure(background="white")
Entry6.configure(foreground="#000000")
Entry6.pack()
Entry7 = Entry(root)
Entry7.configure(background="white")
Entry7.pack()
Entry8 = Entry(root)
Entry8.configure(background="white")
Entry8.pack()
Entry9 = Entry(root)
Entry9.configure(background="white")
Entry9.configure(foreground="#000000")
Entry9.pack()
Entry10 = Entry(root)
Entry10.configure(background="white")
Entry10.configure(foreground="#000000")
Entry10.pack()
Entry11 = Entry(root)
Entry11.configure(background="white")
Entry11.configure(foreground="#000000")
Entry11.pack()
Radiobutton1 = Radiobutton(root)
Radiobutton1.configure(justify=LEFT)
Radiobutton1.configure(text='''NRNT-A''')
Radiobutton1.pack()
Radiobutton2 = Radiobutton(root)
Radiobutton2.configure(justify=LEFT)
Radiobutton2.configure(text='''SX-12VP''')
Radiobutton2.pack()
Radiobutton3 = Radiobutton(root)
Radiobutton3.configure(justify=LEFT)
Radiobutton3.configure(text='''DX-48''')
Radiobutton3.pack()
Radiobutton4 = Radiobutton(root)
Radiobutton4.configure(justify=LEFT)
Radiobutton4.configure(text='''SX-16VP''')
Radiobutton4.pack()
Radiobutton5 = Radiobutton(root)
Radiobutton5.configure(justify=LEFT)
Radiobutton5.configure(text='''SX-16F''')
Radiobutton5.pack()
Radiobutton6 = Radiobutton(root)
Radiobutton6.configure(justify=LEFT)
Radiobutton6.configure(text='''FX-8''')
Radiobutton6.pack()
Button1 = Button(root)
Button1.configure(pady="0")
Button1.configure(text='''NEXT''')
Button1.pack()
mainloop()
You are clearly creating a grid of widgets, so I recommend using grid. It's well documented and easy to use.
These types of layouts are easiest if you group all of your layout code together rather than interlacing it with your widget creation.
I'm not going to rewrite your whole program, but here's the general idea:
from Tkinter import *
root = Tk()
root.geometry("600x200")
root.grid_columnconfigure((0,1), weight=1)
Label3 = Label(root, text="Device IP Address")
Label4 = Label(root, text="Username")
Label5 = Label(root, text="Password")
Entry3 = Entry(root)
Entry4 = Entry(root)
Entry5 = Entry(root)
Label3.grid(row=3, column=0)
Entry3.grid(row=3, column=1, sticky="ew")
Label4.grid(row=4, column=0)
Entry4.grid(row=4, column=1, sticky="ew")
Label5.grid(row=5, column=0)
Entry5.grid(row=5, column=1, sticky="ew")
root.mainloop()
The above code creates this window:

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.

Categories

Resources