I'd like to make a program which draws a cosinus graph in orderd range. But there is an error which I'm not able to repair. Error Message: "z = int(self.entry.get())
AttributeError: program instance has no attribute 'entry" Here is my code:
# -*- coding: utf-8 -*-
from Tkinter import Tk, W, E
from ttk import Label, Button, Frame, Entry,Style
import math
import sys
import matplotlib as mp
class program(Frame):
def __init__(self,main):
Frame.__init__(self,main)
self.main = main
self.initUI()
def initUI(self):
self.main.title('COSINUSEK')
Style().configure('TFrame', background = 'black')
Style().configure('TLabel', background = 'black', foreground = 'blue')
Style().configure("TButton", background = 'red', foreground = 'blue')
self.rowconfigure(0, pad = 3)
self.rowconfigure(1, pad = 3)
self.rowconfigure(2, pad = 3)
self.rowconfigure(3, pad = 3)
self.rowconfigure(4, pad = 3)
self.columnconfigure(0,pad =3)
self.columnconfigure(1,pad =3)
self.columnconfigure(2,pad =3)
self.columnconfigure(3,pad =3)
self.columnconfigure(4,pad =3)
label = Label(self, text = 'Podaj zakres w stopniach').grid(row = 0,column = 3)
od = Label(self, text = ' OD').grid(row = 1, column =0)
do = Label(self, text = ' DO').grid(row = 1, column =4 )
entry = Entry(self, justify = 'center').grid(row = 2,column = 0,columnspan = 2 ,sticky = E+ W)
entry1 = Entry(self, justify = 'center').grid(row = 2,column = 4,columnspan = 2, sticky = E)
button = Button(self, text = 'Ok',command = self.ok).grid(row = 3,column = 3)
button1 = Button(self, text = 'Draw', command = self.dra).grid(row = 4, column = 3)
self.pack()
def run(self):
self.main.mainloop()
def ok(self):
x = []
y = []
z = int(self.entry.get())
w = int(self.entry1.get())
i = w
while i in range(w,z):
x.append(i)
for a in x:
y[a] = math.cos((x[a]*math.pi)/180)
i = i + 0.01
def dra(self):
self.mp.ion()
self.mp.plot(self.x,self.y)
self.mp.title('Wykres')
self.mp.xlabel('x')
self.mp.ylabel('y')
self.mp.draw()
program(Tk()).run()
Replace:
entry = Entry(self, justify = 'center').grid(row = 2,column = 0,columnspan = 2 ,sticky = E+ W)
entry1 = Entry(self, justify = 'center').grid(row = 2,column = 4,columnspan = 2, sticky = E)
to
self.entry = Entry(self, justify = 'center')
self.entry.grid(row = 2,column = 0,columnspan = 2 ,sticky = E+ W)
self.entry1 = Entry(self, justify = 'center')
self.entry1.grid(row = 2,column = 4,columnspan = 2, sticky = E)
Otherwise, at line z = int(self.entry.get()), self.entry would not exist. Also, the grid method doesn't return anything so, if you do everything in one line as you did, you lose your Entry object and affect None to entry.
When making the variables, you'll have to set them as instance variables:
self.entry = Entry(self, justify = 'center').grid(row = 2,column = 0,columnspan = 2 ,sticky = E+ W)
self.entry1 = Entry(self, justify = 'center').grid(row = 2,column = 4,columnspan = 2, sticky = E)
Related
i want it to expand the size accordingly if the window is expanded, so far i use the grid.row/column configure to make its weight =1 but it will be all messed up it i expand the window.
import time
import tkinter as tk
#Initialise the window
clock = tk.Tk()
clock.title('Easy CLock')
clock.configure(bg='#121212')
clock.columnconfigure(0, weight = 1)
clock.rowconfigure(0, weight = 1)
border_effects = {
"flat": tk.FLAT,
"sunken": tk.SUNKEN,
"raised": tk.RAISED,
"groove": tk.GROOVE,
"ridge": tk.RIDGE,
}
#Logo will be under the main parent
logo = tk.PhotoImage(file = r'C:\Users\User\VSC\Alarm\Logo1.png')
logo_size = logo.subsample(5)
#Time and Date function
def time_date():
# current time
current_time = time.strftime('%H:%M:%S')
current_date = time.strftime(r'%m/%d/%Y')
clock.after(200, time_date)
#Displays the time
c_time = tk.Label(f_time, text = current_time, fg='white', bg='#121212', font=('Verdana', 30))
c_date = tk.Label(f_time, text = current_date, font=('Verdana', 10), fg='white', bg='#121212')
c_time.grid(column=0, row=0)
c_date.grid(column=0, row=1)
#alarm button command
def alarm_func():
#Alarm label
c_clicked = tk.Label(f_alarm, text='Alarm Interface', fg='white', bg='#121212')
c_clicked.grid(column=0, row=1, sticky = 'N')
def recall_frame(event):
if event == f_alarm:
event.grid_forget()
f_time.grid(column=0, row =1, columnspan = 4, sticky = 'N')
elif event == f_time:
event.grid_forget()
f_alarm.grid(column=0, row=1, columnspan = 4, rowspan = 2)
def back_func():
pass
#Creating Frames
f_time = tk.Frame(clock) #Clock Button
f_alarm = tk.Frame(clock) #Alarm Buttton
#configure the frames
f_time.configure(bg = '#121212')
f_alarm.configure(bg = '#121212')
#Setting label in the frame
f_lbl = tk.Label(clock, text= ' Simplistic Clock', image = logo_size, font=('Verdana', 30), fg='white', bg='#121212', compound = tk.LEFT)
time_but = tk.Button(clock, text='Clock', command= lambda :[time_date(), recall_frame(f_alarm)], bg='#f39c12', width = 15, relief = border_effects['ridge'])
alarm_but = tk.Button(clock, text = 'Alarm', command = lambda :[alarm_func(), recall_frame(f_time)], bg='#f39c12', width = 15, relief = border_effects['ridge'])
quit_but = tk.Button(clock, text='Exit', command = clock.quit, bg='#f39c12', width = 15, relief = border_effects['ridge'])
back_but = tk.Button(clock, text = 'Back To Home', command = back_func, bg='#f39c12', width = 15, relief = border_effects['ridge'])
f_lbl.config(borderwidth = 4, relief = border_effects['sunken'])
f_lbl.grid_columnconfigure(0, weight = 1)
#Putting it on the frames
f_lbl.grid(column = 0, row = 0, columnspan = 4, sticky = 'N')
time_but.grid(column = 0, row = 3)
alarm_but.grid(column = 1, row = 3)
back_but.grid(column = 2, row = 3)
quit_but.grid(column = 3, row = 3)
clock.mainloop()
also, why does the border in the f_lbl, simplistic clock not fully extended through out all 4 column since i put columnspan = 4 , and weight = 1 , should't it expand fully through all 4 columns?
I have a problem with grid manager in Python Tkinter.
I was add a Frame and labels inside it appeared in center instead of left top.
What is the problem with my code?
Thanks.
GUIImage
from tkinter import *
class PacijentGUI(Tk):
def __init__(self):
super().__init__()
self.napraviGUI()
def napraviGUI(self):
#Listbox
self.__pacijentiListbox = Listbox(self,width=35, height=15)
self.__pacijentiListbox.grid(row=0,column = 0,sticky = E)
#Frame
panelZaPrikaz = Frame(self, borderwidth = 2, relief = "ridge", padx=100,pady = 78)
panelZaPrikaz.grid(row=0,column = 1,sticky = E)
#Labels in frame
Label(panelZaPrikaz,text = "LBO: ").grid(row = 0,column = 0)
Label(panelZaPrikaz,text = "Ime: ").grid(row = 1,column = 0)
Label(panelZaPrikaz,text="Prezime: ").grid(row = 2,column = 0)
Label(panelZaPrikaz,text = "Datum rodjenja: ").grid(row = 3,column=0)
def main():
app = PacijentGUI()
app.geometry("800x600")
app.title('Evidencija')
app.mainloop()
if __name__ == '__main__':
main()
I am creating a currency converter that asks the user to choose the starting and ending currencies with two optionmenu widgets. The problem I have run into is after the currency has been converted, I want to create a button that reverses the optionmenu values to convert back to the original currency. For example, I originally convert 20 USD to EUR. I want the button to reverse it to convert 20 EUR to USD and reflect the change in the optionmenus. Here is the code I have:
currency_list = []
infile = open('currency_list.txt', 'r')
for currency in infile:
currency_list[:-1]
currency_list.append(currency.strip("\t\n\t"))
initial1 = currency_list[-16] # initially set first option menu to USD from currency list
initial2 = currency_list[43] # initially set second option menu to EUR from currency list
my_list = [currency_list[-16], currency_list[43]]
class CurrencyConverter(tk.Frame):
def reverse(self):
my_list.reverse()
print (my_list)
self.currency_1_menu = tk.OptionMenu(self, self.currency_1, *currency_list)
self.currency_2_menu = tk.OptionMenu(self, self.currency_2, *currency_list)
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
body_color = "white"
body_font_color = "black"
entry_color = body_color
entry_font_color = "gray26"
self.currency_1 = tk.StringVar() # currency to convert from
self.currency_2 = tk.StringVar() # currency to convert to
self.currency_1.set(my_list[0]) # set value from list above
self.currency_2.set(my_list[1]) # set value from list above
self.answer = tk.StringVar() # result from conversion
self.value_id = tk.StringVar() # number user types in to convert from
self.header_text = tk.StringVar() # header to display information
logo = tkinter.PhotoImage(file = "logo_copy.gif") # import logo. Not working!!
label = tk.Label(self,image = logo)
label.config(bg = body_color)
label.grid(row = 0, column = 2, rowspan = 2)
self.header = tk.Label(self, textvariable = self.header_text)
self.result_label = tk.Label(self, textvariable = self.answer, fg = body_font_color)
self.value_entry = tk.Entry(self, textvariable = self.value_id)
self.currency_1_menu = tk.OptionMenu(self, self.currency_1, *currency_list)
self.currency_2_menu = tk.OptionMenu(self, self.currency_2, *currency_list)
self.calculate_button = tk.Button(self, command = self.convert, text = 'calculate')
self.reverse_button = tk.Button(self, command = lambda: self.reverse, text = 'reverse')
home_page_button = tk.Button(self, text = "Home Page", command = lambda: controller.show_frame(StartPage))
self.header.config(bg = body_color, font = ('arial', 12), fg = entry_font_color)
self.result_label.config(font = ('Arial', 36))
self.value_entry.config(font = 'bold', justify = 'center', bg = entry_color, fg = entry_font_color, highlightthickness = 0)
self.currency_1_menu.config(bg = body_color, width = 25, height = 2)
self.currency_2_menu.config (bg = body_color, width = 25, height = 2)
self.result_label.config (bg = body_color)
self.calculate_button.config (bg = body_color, highlightbackground = body_color)
self.reverse_button.config (bg = body_color, highlightbackground = body_color)
self.header.grid(row = 0, column = 0, sticky = 'w')
self.result_label.grid(row = 1, column = 0, sticky = 'w')
self.value_entry.grid(row = 2, column = 0)
self.currency_1_menu.grid (row = 2, column = 1)
self.currency_2_menu.grid (row = 3, column = 1)
self.calculate_button.grid(row = 4, column = 0)
self.reverse_button.grid(row = 2, column = 2, rowspan = 2)
home_page_button.grid(row = 4, column = 1)
def convert(self):
self.currency_1_iso = self.currency_1.get()[0:3]
self.currency_2_iso = self.currency_2.get()[0:3]
url = "https://www.xe.com/currencyconverter/convert/?Amount=" + self.value_id.get() + "&From=" + self.currency_1_iso + "&To=" + self.currency_2_iso
print(url)
if (not os.environ.get('PYTHONHTTPSVERIFY', '') and
getattr(ssl, '_create_unverified_context', None)):
ssl._create_default_https_context = ssl._create_unverified_context
html_code = urllib.request.urlopen(url).read()
self.soup = BeautifulSoup(html_code, 'html.parser')
self.result = self.soup.find('span', {'class': "uccResultAmount"}).string
self.answer.set(self.result + " " + self.currency_2_iso)
self.header_text.set(self.value_id.get() + self.currency_1.get()[5:] + ' equals')
Your current attempt at reverse() creates brand new OptionMenus - and never actually displays them. You don't actually need to touch the OptionMenus themselves, just swap the values in the two variables they are tied to:
temp = self.currency_1.get()
self.currency_1.set(self.currency_2.get())
self.currency_2.set(temp)
I have an issue where all my radio buttons are selected when I try to click one of them. It is for a conversion calculator, so once I solve this issue I will be able to carry the same code over for my other conversions. Any help is greatly appreciated.
Thanks,
Jamie
`from tkinter import*
from tkinter import ttk
class GUI:
def __init__(self, root):
notebook = ttk.Notebook(root)
notebook.pack()
self.temp_frame = ttk.Frame(notebook)
self.length_frame = ttk.Frame(notebook)
self.weight_frame = ttk.Frame(notebook)
#-----------------Length------------------------#
notebook.add(self.length_frame, text = "Length")
#Radio Buttons
v = StringVar()
MODES = ["mm","cm","Inch","Feet","Yards","Metre","Km","Miles"]
v.set("0") # initialize
r=0
for r in range(len(MODES)):
b = ttk.Radiobutton(self.length_frame, text=MODES[r], variable=v )
b.grid(row=r ,column = 0, sticky = W)
#Radio Buttons
v1 = StringVar()
MODES1 = ["mm","cm","Inch","Feet","Yards","Metre","Km","Miles"]
v1.set("0")#initialize
r=0
for r in range(len(MODES1)):
b = ttk.Radiobutton(self.length_frame, text=MODES1[r], variable=v1 )
b.grid(row=r ,column = 6, sticky = W)
#Entry Box
self.Text_length_left = StringVar()
self.entry_length_left = ttk.Entry(self.length_frame, textvariable = self.Text_length_left, width = 15)
self.entry_length_left.grid(row = 4, column = 2)
self.Text_length_right = StringVar()
self.entry_length_right = ttk.Entry(self.length_frame, textvariable = self.Text_length_right, width = 15, state = "readonly")
self.entry_length_right.grid(row = 4, column = 4)
#Label
self.label_3 = Label(self.length_frame, text = "From:")
self.label_3.grid(row = 3, column = 2)
self.label_4 = Label(self.length_frame, text = "To:")
self.label_4.grid(row = 3, column = 4)
self.label_1 = Label(self.length_frame, text = "-->")
self.label_1.grid(row = 4, column = 3)
self.label_2 = Label(self.length_frame, text = " ")
self.label_2.grid(row = 4, column = 5)
#---------------------Temp Frame ----------------------#
notebook.add(self.temp_frame, text = "Temperature")
if __name__ == "__main__":
root = Tk()
app = GUI(root)
root.mainloop()`
You never set the value keyword. This is what's stored in the control variable for a group of radiobuttons when clicked.
I am trying to calculate a formula and display its output as a table in TKinter. Since it is not working, I am just trying to get a simple result and print it to a canvas widget. When this gets working I will do the entire loan formula. As it is I get no output in the GUI or in the console.
Is this even possible to place the result of a calculation as text in canvas.create_text?
from tkinter import * # Import tkinter
width = 500
height = 500
class MainGUI:
def __init__(self):
window = Tk() # Create a window
window.title(" Loan Schedule ") # Set title
frame1 = Frame(window)
frame1.grid(row = 1, column = 1)
Label(frame1, text = " Loan Amount ").grid(row = 1, column = 1, sticky = W)
self.v1 = StringVar()
Entry(frame1, textvariable = self.v1, justify = RIGHT).grid(row = 1, column = 2)
Label(frame1, text = " Years ").grid(row = 1, column = 3, sticky = W)
self.v2 = StringVar()
Entry(frame1, textvariable = self.v2, justify = RIGHT).grid(row = 1, column = 4)
btCalculate = Button(frame1, text = " Calculate ", command = self.calculate()).grid(row = 1, column = 5, sticky = E)
frame2 = Frame(window)
frame2.grid(row = 2, column = 1)
self.canvas = Canvas(frame2, width = width, height = height, bg = "white")
self.canvas.pack()
self.canvas.create_text(25, 25, text = self.calculate(), tags = "text")
window.mainloop() # Create an event loop
def calculate(self):
result = self.v1.get() + self.v2.get()
print(result)
return result
MainGUI()
command require function name without ()
command = self.calculate
so now it works
from tkinter import * # Import tkinter
width = 500
height = 500
class MainGUI:
def __init__(self):
window = Tk() # Create a window
window.title(" Loan Schedule ") # Set title
frame1 = Frame(window)
frame1.grid(row = 1, column = 1)
Label(frame1, text = " Loan Amount ").grid(row = 1, column = 1, sticky = W)
self.v1 = StringVar()
Entry(frame1, textvariable = self.v1, justify = RIGHT).grid(row = 1, column = 2)
Label(frame1, text = " Years ").grid(row = 1, column = 3, sticky = W)
self.v2 = StringVar()
Entry(frame1, textvariable = self.v2, justify = RIGHT).grid(row = 1, column = 4)
btCalculate = Button(frame1, text = " Calculate ", command = self.calculate).grid(row = 1, column = 5, sticky = E)
frame2 = Frame(window)
frame2.grid(row = 2, column = 1)
self.canvas = Canvas(frame2, width = width, height = height, bg = "white")
self.canvas.pack()
self.canvas.create_text(55, 10, text = self.add_text(), tags = "text")
window.mainloop() # Create an event loop
def calculate(self):
result = int(self.v1.get()) + int(self.v2.get())
self.canvas.create_text(25, 25, text = result, tags = "text")
print(result)
return result
def add_text(self):
return "HELLO WORLD"
MainGUI()
by the way: line below means - run self.calculate() and result assign to command
command = self.calculate()