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.
Related
in this code below i need to make a button if clicked use the entered number by user in entry widget (innenleiter) and if not, then use the variable formula and proceed.
import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk
import scipy.special as scs
import scipy.optimize as sco
import matplotlib.pyplot as plt
import numpy as np
def button_clear():
eps_r_Entry.delete(0, END)
impedanz_Entry.delete(0, END)
aussenleiter_Entry.delete(0, END)
innenleiter_Entry.delete(0, END)
def result_function():
c0 = 299792458
m0 = 4 * np.pi * 1e-7
e0 = 1 / (m0 * c0 ** 2)
z0 = np.sqrt(m0 / e0)
zl = float(impedanz_Var.get())
eps_r = float(eps_r_Var.get())
aus_d = float(aussenleiter_Var.get())
# in_d = float(innenleiter_Var.get())
''''
here i need a button if clicked use the number entered in Entry widget (innenleiter)
if not, take this variable and proceed (in_d = aus_d / (np.e ** (zl * 2 * np.pi *
np.sqrt(eps_r) / z0))
'''
in_d = aus_d / (np.e ** (zl * 2 * np.pi * np.sqrt(eps_r) / z0))
r1 = np.sqrt(eps_r) * aus_d / 2
r = np.sqrt(eps_r) * in_d / 2
a = r1 / r
z = z0 / (2 * np.pi * np.sqrt(eps_r)) * np.log(aus_d / in_d)
def wav_num(x):
return ((scs.jn(0, x*a) - scs.jn(2, x*a)) * (scs.yn(0, x) - scs.yn(2, x))
- (scs.yn(0, x*a) - scs.yn(2, x*a)) * (scs.jn(0, x) - scs.jn(2, x)))
x = np.arange(0.1, 1.0, 0.01)
test = wav_num(x)
etsi = sco.brentq(wav_num, 0.1, 1.0)
f_g = etsi * (a + 1) / (2 * np.pi * np.sqrt(m0 * e0) * (r1 + r) * 1e-3) / 1e9
result = round(f_g, 2)
result1 = round(z, 2)
result2 = round(in_d, 2)
Result_label.config(text='f_g : ' + str(result))
Result_label1.config(text='impedanz : ' + str(result1))
Result_label2.config(text='innenleiter D/O : ' + str(result2))
# fig = plt.figure(1)
plt.close()
plt.figure(1)
plt.plot(x, test)
plt.grid(True)
plt.savefig("result.png", dpi=70)
logo = Image.open('result.png')
logo = ImageTk.PhotoImage(logo)
logo_label = tk.Label(image=logo)
logo_label.image = logo
logo_label.grid(row=13, column=8, columnspan=4)
return logo
root = tk.Tk()
root.title("Coax TE Mode")
root.geometry("1400x1080")
root.config(background="#145")
var = DoubleVar()
# generate the window
title = Label(root, text="Coax TE Mode", font=('Helvetica', 22), bg="#145", fg="white")
title.grid(row=0, column=3, padx=20, pady=20)
leerZeile = Label(root, text="", bg="#145", fg="#145")
leerZeile.grid(row=1, column=1)
leerZeile = Label(root, text="", bg="#145", fg="#145")
leerZeile.grid(row=2, column=1)
# Impedanz = Zl (in die gleichung als Z gezeichnet)
impedanz_Label = Label(root, text="Impedanz", font=12, bg="#145", fg="white")
impedanz_Label.grid(row=6, column=0, padx=10, pady=10)
impedanz_Var = DoubleVar()
impedanz_Entry = Entry(root, textvariable=impedanz_Var, width=32, borderwidth=5, )
impedanz_Entry.grid(row=6, column=1, columnspan=4, padx=10, pady=10)
# Innenleiter = d (in die gleichung als d gezeigt)
innenleiter_Label = Label(root, text="Innenleiter", font=12, bg="#145", fg="white")
innenleiter_Label.grid(row=4, column=0, padx=10, pady=10)
innenleiter_Var = DoubleVar()
innenleiter_Entry = Entry(root, textvariable=innenleiter_Var, width=32, borderwidth=5)
innenleiter_Entry.grid(row=4, column=1, columnspan=4, padx=10, pady=10)
# Aussenleiter = D (in die gleichung als D gezeigt)
aussenleiter_Label = Label(root, text="Außenleiter", font=12, bg="#145", fg="white")
aussenleiter_Label.grid(row=5, column=0, padx=10, pady=10)
aussenleiter_Var = DoubleVar()
aussenleiter_Entry = Entry(root, textvariable=aussenleiter_Var, width=32, borderwidth=5)
aussenleiter_Entry.grid(row=5, column=1, columnspan=4, padx=10, pady=10)
# eps_r = in die gleichung als Permittivity dry air 1.000594
eps_r_Label = Label(root, text="eps_r", font=12, bg="#145", fg="white")
eps_r_Label.grid(row=7, column=0, padx=10, pady=10)
eps_r_Var = DoubleVar()
eps_r_Entry = Entry(root, textvariable=eps_r_Var, width=32, borderwidth=5)
eps_r_Entry.grid(row=7, column=1, columnspan=4, padx=10, pady=10)
# Result btn
Result_btn = Button(command=result_function, text="Result", bg="#145", fg="white", font=12)
Result_btn.grid(row=8, column=2, padx=10, pady=10)
# Show Result tkinter root window
Result_label = Label(root, text="", bg="#145", fg="white", font=14)
Result_label.grid(row=10, column=2, columnspan=20, padx=2, pady=2)
Result_label1 = Label(root, text="", bg="#145", fg="white", font=14)
Result_label1.grid(row=11, column=2, columnspan=20, padx=2, pady=2)
Result_label2 = Label(root, text="", bg="#145", fg="white", font=14)
Result_label2.grid(row=12, column=2, columnspan=20, padx=2, pady=2)
# clear btn soll entry fields räumen
plt_btn = Button(text="clear", command=button_clear, bg="#145", fg="white", font=12)
plt_btn.grid(row=8, column=3, padx=10, pady=10)
# Exit
exit_btn = Button(root, text="Exit", bg="#145", fg="white", font=12, command=root.quit)
exit_btn.grid(row=8, column=4, padx=10, pady=10)
root.mainloop()
So this seems like a simple issue but let me know if I misunderstood. What you want to do is disable and enable a text widget. To do this you would use some_text_widget.config(state="enabled") to enable the widget and/or some_text_widget.config(state="disabled"). Does this work for you?
I want to create a program that is a Bakery online shop but I'm stuck on a show bill page, I want to show the menu list, Amount, Price, Total of that cake or drink in one line and the last line is all total
this my code :
from tkinter import*
def mainwindow():
main = Tk()
x = main.winfo_screenwidth() / 2 - (w / 2)
y = main.winfo_screenheight() / 2 - (h / 2)
main.geometry("%dx%d+%d+%d" % (w, h, x, y))
main.title("Cake_Shop by Winit Auppkarasakun")
main.option_add("*font", "times 17 bold")
menubar = Menu(main)
filemenu = Menu(menubar, tearoff=False)
filemenu.add_command(label="Menu", command=menu)
filemenu.add_command(label="Checkout", command=checkout)
filemenu.add_command(label="Exit", command=main.quit)
menubar.add_cascade(label="File", menu=filemenu)
main.configure(bg="pink", menu=menubar)
return main
def menu():
frame1 = Frame(main, bg="yellow")
frame1.place(x=0, y=0, width=w, height=h)
frame1.columnconfigure((0,1), weight=1)
frame1.rowconfigure((0,1,2,3,4,5,6,7,8,9), weight=1)
Label(frame1, text="Strawberry Cake : ", bg="yellow", fg="blue", font="times 18 bold").grid(row=0, column=0)
Label(frame1, image = img_menu_1, bg="yellow").grid(row=1, column=0)
Label(frame1, text="Price 85", bg="yellow", fg="black").grid(row=2, column=0)
Spinbox(frame1, from_=0, to=10, textvariable=cake_spy1).grid(row=3, column=0)
Label(frame1, text="Cheese Cake : ", bg="yellow", fg="blue", font="times 18 bold").grid(row=4, column=0)
Label(frame1, image = img_menu_2, bg="yellow").grid(row=5, column=0)
Label(frame1, text="Price 95", bg="yellow", fg="black").grid(row=6, column=0)
Spinbox(frame1, from_=0, to=10, textvariable=cake_spy2).grid(row=7, column=0)
Label(frame1, text="Strawberry Mixed : ", bg="yellow", fg="blue", font="times 18 bold").grid(row=0, column=1)
Label(frame1, image = img_drink_1, bg="yellow").grid(row=1, column=1)
Label(frame1, text="Price 120", bg="yellow", fg="black").grid(row=2, column=1)
Spinbox(frame1, from_=0, to=10, textvariable=drink_spy1).grid(row=3, column=1)
Label(frame1, text="Orange Mixed : ", bg="yellow", fg="blue", font="times 18 bold").grid(row=4, column=1)
Label(frame1, image = img_drink_2, bg="yellow").grid(row=5, column=1)
Label(frame1, text="Price 140", bg="yellow", fg="black").grid(row=6, column=1)
Spinbox(frame1, from_=0, to=10, textvariable=drink_spy2).grid(row=7, column=1)
def checkout():
frame2 = Frame(main, bg="lightgreen")
frame2.place(x=0, y=0, width=w, height=h)
frame2.rowconfigure((0,1,2,3,4,5), weight=1)
frame2.columnconfigure((0,1,2,3), weight=1)
Label(frame2, text="Menu list", bg="lightgreen", fg="black").grid(row=0, column=0, sticky=N, pady=10)
Label(frame2, text="Amount", bg="lightgreen", fg="black").grid(row=0, column=1, sticky=N, pady=10)
Label(frame2, text="Price", bg="lightgreen", fg="black").grid(row=0, column=2, sticky=N, pady=10)
Label(frame2, text="Total(Baths)", bg="lightgreen", fg="black").grid(row=0, column=3, sticky=N, pady=10)
for i,des in enumerate(name_menu_list):
Label(frame2, text=des["Menu list"]).grid(row=i+1, column=0, sticky="news")
Label(frame2, text=des["Amount"]).grid(row=i+1, column=1, sticky="news")
Label(frame2, text=des["Price"]).grid(row=i+1, column=2, sticky="news")
Label(frame2, text=des["Total(Baths)"]).grid(row=i+1, column=3, sticky="news")
def calculate():
global total
global menu_total
menu_total = 0
total = 0
if cake_spy1.get() > 0:
menu_total = cake_spy1.get() * 85
total = total + menu_total
show_menu = {"Menu list":"Strawberry Cake", "Amount":cake_spy1, "Price":85, "Total(Baths)":menu_total}
elif cake_spy2.get() > 0:
menu_total = cake_spy2.get() * 95
total = total + menu_total
show_menu = {"Menu list":"Cheese Cake", "Amount":cake_spy2, "Price":95, "Total(Baths)":menu_total}
elif drink_spy1.get() > 0:
menu_total = drink_spy1.get() * 120
total = total + menu_total
show_menu = {"Menu list":"Strawberry Mixed", "Amount":drink_spy1, "Price":120, "Total(Baths)":menu_total}
elif drink_spy2.get() > 0:
menu_total = drink_spy2.get() * 140
total = total + menu_total
show_menu = {"Menu list":"Orange Mixed", "Amount":drink_spy2, "Price":140, "Total(Baths)":menu_total}
name_menu_list.append(show_menu)
menu_total = 0
total = 0
w = 800
h = 700
main = mainwindow()
cake_spy1 = StringVar()
cake_spy2 = StringVar()
drink_spy1 = StringVar()
drink_spy2 = StringVar()
frame0 = Frame(main, bg="pink") #for homepage
frame0.grid(row=0, column=0, sticky="news")
img_home = PhotoImage(file="images/myshop.png")
Label(frame0, image=img_home, bg="pink").grid(row=0, column=0, sticky="news", padx=130, pady=40)
Label(frame0, text="...Welcome to my resturent...", bg="pink", fg="black", font="times 25 bold").grid(row=1, column=0)
img_menu_1 = PhotoImage(file="images/cake1.png")
img_menu_2 = PhotoImage(file="images/cake2.png")
img_drink_1 = PhotoImage(file="images/drink1.png")
img_drink_2 = PhotoImage(file="images/drink2.png")
name_menu_list = []
menu_name = StringVar()
main.mainloop()
this all my code the problem function is checkout and calculate
Thank you for helping me
So I am trying to create this function to make a plot of a mass spring damper system and we should be able to basically change whatever we like, the values for the function and the sliders should affect the plot as well, i'm having trouble trying to get my function plotted. The error is coming within the function where I am trying to use my solveMBK function inside my PlotWindow function, I am not sure how to include the values from my sliders as well as the other values that are inputted in the start up window. Here is a look at my code:
import tkinter as tk
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.figure import Figure
import numpy as np
def solveMBK(inlist):
x0 = inlist[0]
dx0 = inlist[1]
dt = inlist[2]
m = inlist[3]
b = inlist[4]
k = inlist[5]
tf = inlist[6]
Z = inlist[7]
t = np.arange(0,tf,dt)
z0 = np.zeros_like(t)
z1 = np.zeros_like(t)
z0[0] = x0
z1[0] = dx0
for c in range(len(t)-1):
z0[c+1] = z0[c] + z1[c]*dt
z1[c+1] = z1[c] + ((ABradiobutton(Z,t[c]) - k*z0[c] - b*z1[c]) / m)
x = z0
return t,x
def ABradiobutton(Z, t):
if Z == 1:
A = float(A_entry.get())
return A
elif Z == 2:
B = float(B_entry.get())
return np.sin(B*t)
def PlotWindow():
root1 = tk.Tk()
root1.title("Plot")
Mmin = float(Mmin_entry.get())
Mmax = float(Mmax_entry.get())
bmin = float(bmin_entry.get())
bmax = float(bmax_entry.get())
kmin = float(kmin_entry.get())
kmax = float(kmax_entry.get())
mscale = tk.Scale(root1, from_=Mmin, to=Mmax, label="m", bd=2, length=200, orient=tk.HORIZONTAL, command = funcPlot)
mscale.set((Mmin+Mmax)/2)
mscale.grid(row=1, column=0)
bscale = tk.Scale(root1, from_=bmin, to=bmax, label="b", bd=2, length=200, orient=tk.HORIZONTAL, command = funcPlot)
bscale.set((bmin+bmax)/2)
bscale.grid(row=3, column=0)
kscale = tk.Scale(root1, from_=kmin, to=kmax, label="k", bd=2, length=200, orient=tk.HORIZONTAL, command = funcPlot)
kscale.set((kmin+kmax)/2)
kscale.grid(row=5, column=0)
tk.Label(root1, text = " ").grid(row=6, column=0)
tk.Button(root1, text="Back", command=root1.destroy).grid(row=7, column=0)
Graph_Frame = tk.Frame(root1)
Graph_Frame.grid(row=2, column=2, columnspan=10, rowspan=10)
Fig = Figure(figsize=(5.5,4))
a = Fig.add_subplot(111)
if Radio_Var == 1:
t,x = solveMBK(str(mscale.get()), str(bscale.get()), str(kscale.get()), str(A_entry.get()), str(x0_Entry.get()), str(dxdt_Entry.get()), str(tfinal_entry.get()), str(dt_entry.get()))
a.plot(t,x)
# elif Radio_Var == 2:
# t,x = solveMBK()
# a.plot(t,x)
tk.Label(Graph_Frame, text = "Mass-Spring-Damper Plot").pack()
canvas = FigureCanvasTkAgg(Fig, Graph_Frame)
canvas.draw()
canvas.get_tk_widget().pack()
toolbar = NavigationToolbar2Tk(canvas, Graph_Frame)
toolbar.update()
canvas.get_tk_widget().pack()
def CloseWindow():
root.quit()
root.destroy()
exit()
def funcPlot(input_list, mscale, bscale, kscale, a, canvas, event=None):
input_list[0]=float(x0_Entry.get())
input_list[1]=float(dxdt_Entry.get())
input_list[2]=float(dt_entry.get())
input_list[3]=float(mscale.get())
input_list[4]=float(bscale.get())
input_list[5]=float(kscale.get())
input_list[6]=float(tfinal_entry.get())
input_list[7]=float(Radio_Var.get())
data = solveMBK(input_list)
a.plot(data[0], data[1])
canvas.draw()
return
root = tk.Tk()
root.title("Numerical solution of a second order differential equation")
tk.Label(root, text = "Differential Equation:").grid(row=0, column=0, sticky=tk.E)
tk.Label(root, text = "m d2x/dt2 + b dx/dt + kx = f(x)").grid(row=0, column=1)
x0_Start = tk.IntVar()
x0_Start.set("0")
x0_Entry = tk.Entry(root, width=7, textvariable = x0_Start)
tk.Label(root, text = "x(0) = ").grid(row=1, column=0, stick=tk.E), x0_Entry.grid(row=1, column=1, sticky=tk.W)
dxdt_Start = tk.IntVar()
dxdt_Start.set("0")
dxdt_Entry = tk.Entry(root, width=7, textvariable = dxdt_Start)
tk.Label(root, text = "dx(0)/dt= ").grid(row=2, column=0, sticky=tk.E), dxdt_Entry.grid(row=2, column=1, sticky=tk.W)
A_start = tk.IntVar()
A_start.set("1")
A_entry = tk.Entry(root, width=7, textvariable = A_start)
tk.Label(root, text = "A = ").grid(row=6, column=1, sticky=tk.E), A_entry.grid(row=6, column=2, sticky=tk.W)
B_start = tk.IntVar()
B_start.set("0")
B_entry = tk.Entry(root, width=7, textvariable=B_start)
tk.Label(root, text= "B =").grid(row=7,column=1, sticky=tk.E), B_entry.grid(row=7, column=2, sticky=tk.W)
tk.Label(root, text = " ").grid(row=5, column=0, sticky=tk.E)
Radio_Var = tk.IntVar()
tk.Radiobutton(root, text="A", value = 1, variable=Radio_Var).grid(row=6, column=1, sticky = tk.W)
tk.Radiobutton(root, text="sin(Bt)", value = 2, variable=Radio_Var).grid(row=7, column=1, sticky = tk.W)
Radio_Var.set(1)
tk.Label(root, text = "f(x) = ").grid(row=6, column=0, sticky=tk.E)
tk.Label(root, text = " ").grid(row=8, column=0, sticky=tk.E)
tfinal_start = tk.IntVar()
tfinal_start.set("10")
tfinal_entry = tk.Entry(root, width = 7, textvariable=tfinal_start)
tk.Label(root, text = "tfinal = ").grid(row=9, column=0, sticky=tk.E), tfinal_entry.grid(row=9, column=1, sticky=tk.W)
dt_start = tk.IntVar()
dt_start.set("0.001")
dt_entry = tk.Entry(root, width = 7, textvariable=dt_start)
tk.Label(root, text = "dt = ").grid(row=9, column=1, sticky=tk.E), dt_entry.grid(row=9, column=2, sticky=tk.W)
tk.Label(root, text = " ").grid(row=10, column=0, sticky=tk.E)
Mmin_start = tk.IntVar()
Mmin_start.set("1")
Mmin_entry = tk.Entry(root, width=7, textvariable=Mmin_start)
tk.Label(root, text = "Mmin = ").grid(row=11, column=0, sticky=tk.E), Mmin_entry.grid(row=11, column=1, sticky=tk.W)
Mmax_start = tk.IntVar()
Mmax_start.set("100")
Mmax_entry = tk.Entry(root, width=7, textvariable=Mmax_start)
tk.Label(root, text = "Mmax = ").grid(row=11,column=1, sticky=tk.E), Mmax_entry.grid(row=11, column=2, sticky=tk.W)
bmin_start = tk.IntVar()
bmin_start.set("1")
bmin_entry = tk.Entry(root, width=7, textvariable=bmin_start)
tk.Label(root, text = "bmin = ").grid(row=12, column=0, sticky=tk.E), bmin_entry.grid(row=12, column=1, sticky=tk.W)
bmax_start = tk.IntVar()
bmax_start.set("250")
bmax_entry = tk.Entry(root, width=7, textvariable=bmax_start)
tk.Label(root, text= "bmax = ").grid(row=12, column=1, sticky=tk.E), bmax_entry.grid(row=12,column=2,sticky=tk.W)
kmin_start = tk.IntVar()
kmin_start.set("1")
kmin_entry = tk.Entry(root, width=7, textvariable=kmin_start)
tk.Label(root, text= "kmin = ").grid(row=13, column=0, sticky=tk.E), kmin_entry.grid(row=13, column=1, sticky=tk.W)
kmax_start = tk.IntVar()
kmax_start.set("500")
kmax_entry = tk.Entry(root, width=7, textvariable=kmax_start)
tk.Label(root, text="kmax = ").grid(row=13, column=1, sticky=tk.E), kmax_entry.grid(row=13, column=2, sticky=tk.W)
tk.Button(root, text = "Quit", command=CloseWindow, width=10).grid(row=14, column=0)
tk.Button(root, text= "Plot", command=PlotWindow, width=10).grid(row=14, column=3)
root.mainloop()
Any help would be appreciated, thank you!
You're passing funcPlot as the command for Scale to call:
mscale = tk.Scale(root1, ..., command=funcPlot)
According to the documentation, command is:
A procedure to be called every time the slider is moved. This
procedure will be passed one argument, the new scale value.
But instead of taking one argument, your funcPlot() function requires six arguments:
def funcPlot(input_list, mscale, bscale, kscale, a, canvas, event=None):
Which leads to the error:
TypeError: funcPlot() missing 5 required positional arguments: 'mscale', 'bscale', 'kscale', 'a', and 'canvas'
You need to rethink how this is supposed to work. Below is my rework of your code. I got around the problem above by using global variables -- I'm not proud of that. I also had to rework the way your plot gets embedded in Tk as what you had wasn't working:
import tkinter as tk
import numpy as np
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.figure import Figure
def solveMBK(inlist):
x0, dx0, dt, m, b, k, tf, Z = inlist
t = np.arange(0, tf, dt)
z0 = np.zeros_like(t)
z1 = np.zeros_like(t)
z0[0] = x0
z1[0] = dx0
for c in range(len(t) - 1):
z0[c + 1] = z0[c] + z1[c] * dt
z1[c + 1] = z1[c] + ((ABradiobutton(Z, t[c]) - k * z0[c] - b * z1[c]) / m)
return t, z0
def ABradiobutton(Z, t):
if Z == 1:
A = float(A_entry.get())
return A
if Z == 2:
B = float(B_entry.get())
return np.sin(B * t)
def PlotWindow():
global mscale, bscale, kscale, subplot, figure_canvas
plot_window = tk.Toplevel(root)
plot_window.title("Plot")
Mmin = float(Mmin_entry.get())
Mmax = float(Mmax_entry.get())
bmin = float(bmin_entry.get())
bmax = float(bmax_entry.get())
kmin = float(kmin_entry.get())
kmax = float(kmax_entry.get())
mscale = tk.Scale(plot_window, from_=Mmin, to=Mmax, label="m", bd=2, length=200, orient=tk.HORIZONTAL, command=funcPlot)
mscale.set((Mmin + Mmax) / 2)
mscale.grid(row=1, column=0)
bscale = tk.Scale(plot_window, from_=bmin, to=bmax, label="b", bd=2, length=200, orient=tk.HORIZONTAL, command=funcPlot)
bscale.set((bmin + bmax) / 2)
bscale.grid(row=3, column=0)
kscale = tk.Scale(plot_window, from_=kmin, to=kmax, label="k", bd=2, length=200, orient=tk.HORIZONTAL, command=funcPlot)
kscale.set((kmin + kmax) / 2)
kscale.grid(row=5, column=0)
tk.Label(plot_window, text=" ").grid(row=6, column=0)
tk.Button(plot_window, text="Back", command=plot_window.destroy).grid(row=7, column=0)
graph_frame = tk.Frame(plot_window)
graph_frame.grid(row=2, column=2, columnspan=10, rowspan=10)
figure = Figure(figsize=(5.5, 4))
subplot = figure.add_subplot(111)
if Radio_Var.get() == 1:
t, x = solveMBK([float(mscale.get()), float(bscale.get()), float(kscale.get()), float(A_entry.get()), float(x0_Entry.get()), float(dxdt_Entry.get()), float(tfinal_entry.get()), float(dt_entry.get())])
subplot.plot(t, x)
# elif Radio_Var.get() == 2:
# t, x = solveMBK()
# subplot.plot(t, x)
figure_canvas = FigureCanvasTkAgg(figure, master=graph_frame)
figure_canvas.draw()
figure_canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
toolbar = NavigationToolbar2Tk(figure_canvas, graph_frame)
toolbar.update()
figure_canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
tk.Label(graph_frame, text="Mass-Spring-Damper Plot").pack()
def CloseWindow():
root.quit()
root.destroy()
exit()
def funcPlot(event):
input_list = []
input_list.append(float(x0_Entry.get()))
input_list.append(float(dxdt_Entry.get()))
input_list.append(float(dt_entry.get()))
input_list.append(float(mscale.get()))
input_list.append(float(bscale.get()))
input_list.append(float(kscale.get()))
input_list.append(float(tfinal_entry.get()))
input_list.append(float(Radio_Var.get()))
t, x = solveMBK(input_list)
subplot.plot(t, x)
figure_canvas.draw()
figure_canvas = subplot = mscale = bscale = kscale = None
root = tk.Tk()
root.title("Numerical solution of a second order differential equation")
tk.Label(root, text="Differential Equation:").grid(row=0, column=0, sticky=tk.E)
tk.Label(root, text="m d2x/dt2 + b dx/dt + kx = f(x)").grid(row=0, column=1)
tk.Label(root, text="x(0) = ").grid(row=1, column=0, stick=tk.E)
x0_Start = tk.IntVar()
x0_Start.set("0")
x0_Entry = tk.Entry(root, width=7, textvariable=x0_Start)
x0_Entry.grid(row=1, column=1, sticky=tk.W)
tk.Label(root, text="dx(0)/dt = ").grid(row=2, column=0, sticky=tk.E)
dxdt_Start = tk.IntVar()
dxdt_Start.set("0")
dxdt_Entry = tk.Entry(root, width=7, textvariable=dxdt_Start)
dxdt_Entry.grid(row=2, column=1, sticky=tk.W)
tk.Label(root, text=" ").grid(row=5, column=0, sticky=tk.E)
Radio_Var = tk.IntVar()
Radio_Var.set(1)
tk.Label(root, text="f(x) = ").grid(row=6, column=0, sticky=tk.E)
tk.Radiobutton(root, text="A", value=1, variable=Radio_Var).grid(row=6, column=1, sticky=tk.W)
tk.Label(root, text="A = ").grid(row=6, column=1, sticky=tk.E)
A_start = tk.IntVar()
A_start.set("1")
A_entry = tk.Entry(root, width=7, textvariable=A_start)
A_entry.grid(row=6, column=2, sticky=tk.W)
tk.Radiobutton(root, text="sin(Bt)", value=2, variable=Radio_Var).grid(row=7, column=1, sticky=tk.W)
tk.Label(root, text="B =").grid(row=7, column=1, sticky=tk.E)
B_start = tk.IntVar()
B_start.set("0")
B_entry = tk.Entry(root, width=7, textvariable=B_start)
B_entry.grid(row=7, column=2, sticky=tk.W)
tk.Label(root, text=" ").grid(row=8, column=0, sticky=tk.E)
tk.Label(root, text="tfinal = ").grid(row=9, column=0, sticky=tk.E)
tfinal_start = tk.IntVar()
tfinal_start.set("10")
tfinal_entry = tk.Entry(root, width=7, textvariable=tfinal_start)
tfinal_entry.grid(row=9, column=1, sticky=tk.W)
tk.Label(root, text="dt = ").grid(row=9, column=1, sticky=tk.E)
dt_start = tk.IntVar()
dt_start.set("0.001")
dt_entry = tk.Entry(root, width=7, textvariable=dt_start)
dt_entry.grid(row=9, column=2, sticky=tk.W)
tk.Label(root, text=" ").grid(row=10, column=0, sticky=tk.E)
tk.Label(root, text="Mmin = ").grid(row=11, column=0, sticky=tk.E)
Mmin_start = tk.IntVar()
Mmin_start.set("1")
Mmin_entry = tk.Entry(root, width=7, textvariable=Mmin_start)
Mmin_entry.grid(row=11, column=1, sticky=tk.W)
tk.Label(root, text="Mmax = ").grid(row=11, column=1, sticky=tk.E)
Mmax_start = tk.IntVar()
Mmax_start.set("100")
Mmax_entry = tk.Entry(root, width=7, textvariable=Mmax_start)
Mmax_entry.grid(row=11, column=2, sticky=tk.W)
tk.Label(root, text="bmin = ").grid(row=12, column=0, sticky=tk.E)
bmin_start = tk.IntVar()
bmin_start.set("1")
bmin_entry = tk.Entry(root, width=7, textvariable=bmin_start)
bmin_entry.grid(row=12, column=1, sticky=tk.W)
tk.Label(root, text="bmax = ").grid(row=12, column=1, sticky=tk.E)
bmax_start = tk.IntVar()
bmax_start.set("250")
bmax_entry = tk.Entry(root, width=7, textvariable=bmax_start)
bmax_entry.grid(row=12, column=2, sticky=tk.W)
tk.Label(root, text="kmin = ").grid(row=13, column=0, sticky=tk.E)
kmin_start = tk.IntVar()
kmin_start.set("1")
kmin_entry = tk.Entry(root, width=7, textvariable=kmin_start)
kmin_entry.grid(row=13, column=1, sticky=tk.W)
tk.Label(root, text="kmax = ").grid(row=13, column=1, sticky=tk.E)
kmax_start = tk.IntVar()
kmax_start.set("500")
kmax_entry = tk.Entry(root, width=7, textvariable=kmax_start)
kmax_entry.grid(row=13, column=2, sticky=tk.W)
tk.Button(root, text="Quit", command=CloseWindow, width=10).grid(row=14, column=0)
tk.Button(root, text="Plot", command=PlotWindow, width=10).grid(row=14, column=3)
root.mainloop()
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()
I could not convert the entry widget values into float for the calculation inside function.
from tkinter import *
def bac_calc():
#gender_condition
gend=gender.get()
if gend == "Male":
k1=0.015
k2=0.080
elif gend == "Female":
k1=0.020
k2=0.075
#weight
kg=body_weight.get()
kg=float(kg)
wt_lbs=kg*2.20462
bac=k1*k2*wt_lbs*100
t= "weight in pound is " , float(bac)
Label(master, text=t).grid(row=23, column=1)
master = Tk()
master.title('Blood Alcohol Level calculator')
#Gender
gender=StringVar()
Label(master, text="Select your gender").grid(row=2, column=0, sticky=W, pady=5)
r1=Radiobutton(master, text="Male", padx=20, variable=gender, value="Male", command=bac_calc).grid(row=3, column=2, sticky=W, pady=5)#anchor
r2=Radiobutton(master, text="Female", padx=20, variable=gender, value="Female", command=bac_calc).grid(row=3, column=3, sticky=W, pady=5)
#Body weight
Label(master, text="Enter the body weight kg").grid(row=5, column=0, sticky=W, pady=5)
body_weight=Entry(master)
body_weight.grid(row=6, column=2)
#Evaluate and Quit
Button(master, text="Evaluate", command=bac_calc).grid(row=8, column=2, sticky=W, pady=5)
Button(master, text="Quit ", command=master.quit).grid(row=9, column=4, sticky=W, pady=5)
master.mainloop()
Can you help me with the problem?
Thanks in advance!!
Sorry for a long code. Anyways,the there are only input widgets and calculations.
As suggested by #PM 2Ring, you should consider adding a default value to the Entry element represented by body_weight. You can use the insert method to do this after creating body_weight.
See here.
Ive cleaned up your code a little bit and I've added an error handling statement for your float conversion problem. One thing you might consider doing is finding the formula for BAC calculations that utilizes kilograms instead of converting it to pounds. Also I would recommend looking up the PEP8 style guides. It helps keep code readable. Hope this helps :)
from tkinter import *
def bac_calc():
# gender_condition
gend = gender.get()
if gend == "Male":
k1 = 0.015
k2 = 0.080
else:
k1 = 0.020
k2 = 0.075
# weight
try:
kg = body_weight.get()
kg = float(kg)
wt_lbs = kg * 2.20462
bac = k1 * k2 * wt_lbs * 100
t = "weight in pound is ", float(bac)
Label(master, text=t).grid(row=23, column=1)
except ValueError as e:
Label(master, text='Input must be numerical.').grid(row=23, column=1)
master = Tk()
master.title('Blood Alcohol Level calculator')
# Gender
gender = StringVar()
Label(master, text="Select your gender").grid(row=2, column=0, sticky=W, pady=5)
r1 = Radiobutton(master, text="Male", padx=20, variable=gender, value="Male", command=bac_calc).grid(row=3, column=2,
sticky=W,
pady=5) # anchor
r2 = Radiobutton(master, text="Female", padx=20, variable=gender, value="Female", command=bac_calc).grid(row=3,
column=3,
sticky=W,
pady=5)
# Body weight
Label(master, text="Enter the body weight kg").grid(row=5, column=0, sticky=W, pady=5)
body_weight = Entry(master)
body_weight.grid(row=6, column=2)
# Evaluate and Quit
Button(master, text="Evaluate", command=bac_calc).grid(row=8, column=2, sticky=W, pady=5)
Button(master, text="Quit ", command=master.quit).grid(row=9, column=4, sticky=W, pady=5)
master.mainloop()