Related
I'm trying to write an app that computes a figure after compound interest over time, but one of my entry boxes isn't showing up. The get function appears to work but the second entry box won't display:
from tkinter import *
from tkinter import ttk
def main():
root = Tk()
root.geometry("600x200")
#frm = ttk.Frame(root, padding=100)
e1 = ttk.Entry(root, width=20, rowspan=1)
e2 = ttk.Entry(root, width=20, rowspan=1)
e3 = ttk.Entry(root, width=20, rowspan=1)
e11 = e1.get()
e22 = e2.get()
e33 = e3.get()
e1.grid(column=1, row=0)
e2.grid(column=1, row=1)
e2.grid(column=1, row=2)
Label(root, text="Amount to invest:").grid(column=0, row=0)
Label(root, text="Number of years invested:").grid(column=0, row=1)
Label(root, text="Add yearly return in percent:").grid(column=0, row=2)
submit = Button(root, text="Submit", command=lambda: Label(root, int(e11)*(int(e22)**int(e33))), padx=10, bg="cyan", fg="Red")
submit.grid(column=1, row=3)
enter code here
root.mainloop()
if __name__ == '__main__':
main()
Your code works, but I removed the rowspan parameter
e1 = ttk.Entry(root, width=20, )
e2 = ttk.Entry(root, width=20, )
e3 = ttk.Entry(root, width=20, )
e11 = e1.get()
e22 = e2.get()
e33 = e3.get()
e1.grid(column=1, row=0)
e2.grid(column=1, row=1)
e3.grid(column=1, row=2)
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()
The buttons, for some reason, seem to be positioned wrong. I checked my code, and it's written to have the correct positioning, but it for some reason is showing all of the buttons except for the "View All" buttons to be too low. The buttons are supposed to be right above each other. Could ya'll help and let me know what the problem is?
from tkinter import *
window = Tk()
#Labels
l1 = Label(window, text="Title")
l1.grid(row=0, column=0)
l2 = Label(window, text="Author")
l2.grid(row=0, column=2)
l3 = Label(window, text="Year")
l3.grid(row=1, column=0)
l4 = Label(window, text="ISBN")
l4 .grid(row=1, column=2)
#Entry boxes
title_text=StringVar()
e1 = Entry(window, textvariable=title_text)
e1.grid(row=0, column=1)
author_text=StringVar()
e2 = Entry(window, textvariable=author_text)
e2.grid(row=0, column=3)
year_text=StringVar()
e3 = Entry(window, textvariable=year_text)
e3.grid(row=1, column=1)
isbn_text=StringVar()
e4 = Entry(window, textvariable=isbn_text)
e4.grid(row=1, column=3)
#listbox
ls1 = Listbox(window, height=6,width=35)
ls1.grid(row=2, column=0)
#Scrollbar
scr1 = Scrollbar(window)
scr1.grid(row=2, column=2, rowspan=6)
#Making the scrolbar scroll down the listbox
ls1.configure(yscrollcommand=scr1.set)
scr1.configure(command=ls1.yview)
#Buttons and stuff
b1 = Button(window, text="View all", width=12)
b1.grid(row=2, column=3)
b2 = Button(window, text="Search Entry", width=12)
b2.grid(row=3, column=3)
b3 = Button(window, text="Add Entry", width=12)
b3.grid(row=4, column=3)
b4 = Button(window, text="Update Selected", width=12)
b4.grid(row=5, column=3)
b5 = Button(window, text="Delete Selected", width=12)
b5.grid(row=6, column=3)
b6 = Button(window, text="Exit", width=12)
b6.grid(row=7, column=3)
window.mainloop()
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()
I tried to get all information about a computer by python but there is no good library to find something like monitor or keyboard or details of graphic card.
Is it possible to get list of hardware or devices of computers?
I personally find the psutil library interesting for monitoring everything that is going on on your system:
https://github.com/giampaolo/psutil
Moreover, you might have a look at the platform lib, which you can use to gather information, guess what, platform, yes.
https://docs.python.org/2/library/platform.html
If you're on linux, it might be a solution to use some system command like lspci and friends to gather other information as well. See, for instance this: http://www.commandlinefu.com/commands/using/lspci
There's also a similar question on stack overflow: Python, In linux obtain VGA specifications via lspci or HAL?
You can use psutil. Here is a good example that I have created long time ago in Python 2.7:
from Tkinter import *
import psutil
import datetime
from multiprocessing import cpu_count
def timeFunc():
time = datetime.datetime.now().strftime("%I:%M:%S %p")
date = datetime.datetime.now().strftime("%Y-%m-%d")
Label(master, text="System Time").grid(row=16, columnspan=2, sticky='w')
e13 = Entry(master)
e13.grid(row=16, column=1)
e13.insert(10, time)
Label(master, text="System Date").grid(row=17, columnspan=2, sticky='w')
e14 = Entry(master)
e14.grid(row=17, column=1)
e14.insert(10, date)
def secs2hours(secs):
mm, ss = divmod(secs, 60)
hh, mm = divmod(mm, 60)
return "%d:%02d:%02d (H:M:S)" % (hh, mm, ss)
def bat():
if not hasattr(psutil, "sensors_battery"):
# outputList.append("Platform not supported")
Label(master, text="Status").grid(row=10, columnspan=2, sticky='w')
e9 = Entry(master)
e9.grid(row=11, column=1)
e9.insert(10, "Platform not supported")
batt = psutil.sensors_battery()
if batt is None:
# outputList.append("No battery is installed")
Label(master, text="Status").grid(row=10, columnspan=2, sticky='w')
e9 = Entry(master)
e9.grid(row=11, column=1)
e9.insert(10, "No battery is installed")
# print("charge: %s%%" % round(batt.percent, 2))
if batt.power_plugged:
Label(master, text="Charge").grid(row=11, columnspan=2, sticky='w')
charge = "%s%%" % round(batt.percent, 2)
e9 = Entry(master)
e9.grid(row=11, column=1)
e9.insert(10, charge)
Label(master, text="Time Left").grid(row=12, columnspan=2, sticky='w')
z = 'N/A'
e10 = Entry(master)
e10.grid(row=12, column=1)
e10.insert(10, z)
Label(master, text="Status").grid(row=13, columnspan=2, sticky='w')
x = ("%s" % (
"Charging" if batt.percent < 100 else "fully charged"
))
e11 = Entry(master)
e11.grid(row=13, column=1)
e11.insert(10, x)
Label(master, text="Plugged in").grid(row=14, columnspan=2, sticky='w')
y = "Yes"
e12 = Entry(master)
e12.grid(row=14, column=1)
e12.insert(10, y)
# outputList.append(y)
# print("plugged in: yes")
else:
Label(master, text="Charge").grid(row=11, columnspan=2, sticky='w')
charge = "%s%%" % round(batt.percent, 2)
e9 = Entry(master)
e9.grid(row=11, column=1)
e9.insert(10, charge)
Label(master, text=" ").grid(row=12, columnspan=2, sticky='w')
Label(master, text="Time Left").grid(row=12, columnspan=2, sticky='w')
x = ("%s" % secs2hours(batt.secsleft))
Label(master, text=" ").grid(row=13, columnspan=2, sticky='w')
Label(master, text="Status").grid(row=13, columnspan=2, sticky='w')
y = "%s" % "discharging"
Label(master, text=" ").grid(row=14, columnspan=2, sticky='w')
Label(master, text="Plugged in").grid(row=14, columnspan=2, sticky='w')
z = "No"
e10 = Entry(master)
e10.grid(row=12, column=1)
e10.insert(10, x)
e11 = Entry(master)
e11.grid(row=13, column=1)
e11.insert(10, y)
e12 = Entry(master)
e12.grid(row=14, column=1)
e12.insert(10, z)
# outputList.append(x)
# print("Plugged in: no")
def main():
global outputList
totalRam = 1.0
totalRam = psutil.virtual_memory()[0] * totalRam
totalRam = str("{:.4f}".format(totalRam / (1024 * 1024 * 1024))) + ' GB'
availRam = 1.0
availRam = psutil.virtual_memory()[1] * availRam
availRam = str("{:.4f}".format(availRam / (1024 * 1024 * 1024))) + ' GB'
ramUsed = 1.0
ramUsed = psutil.virtual_memory()[3] * ramUsed
ramUsed = str("{:.4f}".format(ramUsed / (1024 * 1024 * 1024))) + ' GB'
ramFree = 1.0
ramFree = psutil.virtual_memory()[4] * ramFree
ramFree = str("{:.4f}".format(ramFree / (1024 * 1024 * 1024))) + ' GB'
core = cpu_count()
ramUsages = str(psutil.virtual_memory()[2]) + '%'
cpuPer = str(psutil.cpu_percent()) + '%'
cpuMainCore = psutil.cpu_count(logical=False)
outputList.append(cpuMainCore)
outputList.append(core)
outputList.append(cpuPer)
outputList.append(totalRam)
outputList.append(availRam)
outputList.append(ramUsed)
outputList.append(ramUsages)
outputList.append(ramFree)
def clock():
global outputList
outputList = []
main()
bat()
timeFunc()
master.update_idletasks()
e1 = Entry(master)
e2 = Entry(master)
e3 = Entry(master)
e4 = Entry(master)
e5 = Entry(master)
e6 = Entry(master)
e7 = Entry(master)
e8 = Entry(master)
e1.grid(row=1, column=1)
e2.grid(row=2, column=1)
e3.grid(row=3, column=1)
e4.grid(row=5, column=1)
e5.grid(row=6, column=1)
e6.grid(row=7, column=1)
e7.grid(row=8, column=1)
e8.grid(row=9, column=1)
e1.insert(10, outputList[0])
e2.insert(10, outputList[1])
e3.insert(10, outputList[2])
e4.insert(10, outputList[3])
e5.insert(10, outputList[4])
e6.insert(10, outputList[5])
e7.insert(10, outputList[6])
e8.insert(10, outputList[7])
master.after(1000, clock)
if __name__ == '__main__':
master = Tk()
master.title('System Monitor')
Label(master, text="CPU Info").grid(row=0, columnspan=2, sticky='e')
Label(master, text="Total CPU CORE").grid(row=1, columnspan=2, sticky='w')
Label(master, text="Total Logical Processors").grid(row=2)
Label(master, text="CPU Usages").grid(row=3, columnspan=2, sticky='w')
Label(master, text="RAM Info").grid(row=4, columnspan=2, sticky='e')
Label(master, text="Total RAM").grid(row=5, columnspan=2, sticky='w')
Label(master, text="Available RAM").grid(row=6, columnspan=2, sticky='w')
Label(master, text="RAM Used").grid(row=7, columnspan=2, sticky='w')
Label(master, text="RAM Usages").grid(row=8, columnspan=2, sticky='w')
Label(master, text="RAM Free").grid(row=9, columnspan=2, sticky='w')
Label(master, text="Battery Info").grid(row=10, columnspan=2, sticky='e')
Label(master, text="Additional").grid(row=15, columnspan=2, sticky='e')
Label(master, text=u'\N{COPYRIGHT SIGN}' " foysal_nibir 2018", fg='red').grid(row=19, columnspan=2, sticky='n', )
outputList = []
clock()
master.update_idletasks()
master.mainloop()
Enjoy, have a nice day.