Fixing the design of RadioButtons in Tkinter - python

I want to link my radiobuttons with the title, like merge my title with the radio button. This is the code which I implemented:
but1 = Radiobutton(text = "Current",value = 0)
but1.place(x =400,y = 150)
but2 = Radiobutton(text = "Previous",value = 1)
but2.place(x =320,y = 150)
but3 = Radiobutton(text = "Current",value = 2)
but3.place(x =400,y = 260)
but4 = Radiobutton(text = "Previous",value = 3)
but4.place(x =320,y = 260)
the_window.geometry("510x430")
label1 = Label(the_window,text = "Most-Discussed \n TV SHOW", font =
"Times 10 bold")
label1.place(x = 350,y = 110)
label2 = Label(the_window,text = "Most-Discussed \n TV SHOW", font =
"Times 10 bold")
label2.place(x = 350,y = 230)
This is the actual result:
This is the expected result:

I don't know if you know this, but that widget is called a LabelFrame. See the below example.
P.S. I have changed your geometry manager to grid
import tkinter as tk
root = tk.Tk()
f1 = tk.LabelFrame(root, text="Most-Discussed \n TV SHOW", labelanchor="n", font="Verdana 12 bold", bd=4)
f1.grid(row=0, column=0, padx=10, pady=10)
f2 = tk.LabelFrame(root, text="Music Vinyl \n Album Chart", labelanchor="n", font="Verdana 12 bold", bd=4)
f2.grid(row=1, column=0, padx=10, pady=10)
but1 = tk.Radiobutton(f1, text="Current", value = 0)
but1.grid(row=0, column=0)
but2 = tk.Radiobutton(f1, text="Previous" ,value = 1)
but2.grid(row=0, column=1)
but3 = tk.Radiobutton(f2, text="Current", value = 2)
but3.grid(row=0, column=0)
but4 = tk.Radiobutton(f2, text="Previous" ,value = 3)
but4.grid(row=0, column=1)
root.mainloop()

Related

I am new to tkinter ......Why these entries are not moving when I scrolls?

from tkinter import *
root = Tk()
naan = IntVar()
roti=IntVar()
dal=IntVar()
manchurian = IntVar()
makhani=IntVar()
masala_bhindi = IntVar()
chole = IntVar()
rajma = IntVar()
shahi_panneer = IntVar()
kadahi_paneer = IntVar()
masala_gobhi = IntVar()
allo_gobhi = IntVar()
matar_paneer = IntVar()
menu_roti = "Tava Roti 25 ₹/piece"
menu_dal = "Dal 80 ₹/bowl"
menu_makhani = "Dal Makhni 110 ₹/bowl"
menu_naan = "Naan 50 ₹/piece"
menu_manchurian = "Manchurian 110 ₹/plate"
menu_shahi_panneer = "Shahi paneer 110₹/bowl"
menu_kadahi_paneer = "Kadhai paneer 150/bowl"
menu_masala_gobhi = "Masala gobhi 130₹/bowl"
menu_allo_gobhi = "Aloo gobhi 120₹/bowl"
menu_matar_paneer = "Matar paneer 135₹/bowl"
menu_masala_bhindi = "Masala bhindi 110₹/bowl"
menu_chole = "Chole 100₹/bowl"
menu_rajma = "Rajama 150₹/bowl"
menu_chaap = "Chaap 125₹/bowl"
menu_aloo_parntha = "Aloo parantha 35₹/peice"
menu_cheele = "Cheele 55₹/peice "
scrollbar_tk = Scrollbar(root)
list = Listbox(root, bg="wheat", fg="yellow", width=35, font=("arial", 30), yscrollcommand=scrollbar_tk.set)
list.place(x=100, y=400)
list.insert(END, "Food Items Prices Quantities")
list.insert(END, menu_roti, menu_dal, menu_makhani, menu_naan, menu_manchurian, menu_shahi_panneer)
list.insert(END, menu_kadahi_paneer, menu_masala_gobhi, menu_allo_gobhi, menu_matar_paneer)
list.insert(END, menu_masala_bhindi, menu_chole, menu_rajma, menu_chaap, menu_aloo_parntha, menu_cheele)
q_roti = Entry(root, font=("arial",20), textvariable = roti, fg="Black", width=10)
q_roti.place(x=525, y=115)
q_dal = Entry(root, font=("arial",20), textvariable = dal, fg="black", width=10)
q_dal.place(x=525,y=160)
q_makhani = Entry(root, font=("arial",20), textvariable = makhani, fg="black", width=10)
q_makhani.place(x=525, y=202)
q_naan = Entry(root, font=("arial",20), textvariable = naan, fg="black", width=10)
q_naan.place(x=525,y=245)
q_manchurian = Entry(root,font=("arial",20), textvariable = manchurian, fg="black", width=10)
q_manchurian.place(x=525,y=290)
q_shahi_panneer = Entry(root, font=("arial",20), textvariable = shahi_panneer, fg="black", width=10)
q_shahi_panneer.place(x=525,y=335)
q_kadahi_panneer = Entry(root, font=("arial",20), textvariable = kadahi_paneer, fg="black", width=10)
q_kadahi_panneer.place(x=525,y=380)
q_masala_gobhi = Entry(root, font=("arial",20), textvariable = masala_gobhi, fg="black", width=10)
q_masala_gobhi.place(x=525, y=425)
q_allo_gobhi = Entry(root, font=("arial",20), textvariable = allo_gobhi, fg="black", width=10)
q_allo_gobhi.place(x=525, y=470)
q_matar_panneer = Entry(root, font=("arial",20), textvariable = matar_paneer, fg="black", width=10)
q_matar_panneer.place(x=525,y=515)
q_masala_bhindi = Entry(root, font=("arial",20), textvariable = masala_bhindi, fg="black", width=10)
q_masala_bhindi.place(x=525,y=560)
q_cholle = Entry(root,font=("arial",20), textvariable = chole, fg="black", width=10)
q_cholle.place(x=525, y=605)
q_rajma = Entry(root,font=("arial",20), textvariable = rajma, fg="black", width=10)
q_rajma.place(x=525, y=650)
scrollbar_tk.config(command=list.yview)
list.pack(side=LEFT, fill=BOTH, pady=60, padx=1)
list.itemconfig(0, fg="red")
root.mainloop()
I changed your listboxes to a scrollable frame.
If you would like to be able to select an item like you can in the listbox, you can change the labels to be buttons.
If you want to style the scrollbar, ttk would help.
I reused code from here
from tkinter import *
root = Tk()
root['bg'] = 'wheat'
frame_container=Frame(root, width = 1000)
frame_container['bg'] = 'wheat'
canvas_container=Canvas(frame_container, width = 1000)
canvas_container['bg'] = 'wheat'
frame2=Frame(canvas_container, width = 1000)
frame2['bg'] = 'wheat'
scrollbar_tk = Scrollbar(frame_container, orient="vertical",command=canvas_container.yview)#, yscrollcommand=scrollbar_tk.set
# will be visible if the frame2 is to to big for the canvas
canvas_container.create_window((0,0),window=frame2,anchor='nw')
naan = IntVar()
roti=IntVar()
dal=IntVar()
manchurian = IntVar()
makhani=IntVar()
masala_bhindi = IntVar()
chole = IntVar()
rajma = IntVar()
shahi_panneer = IntVar()
kadahi_paneer = IntVar()
masala_gobhi = IntVar()
allo_gobhi = IntVar()
matar_paneer = IntVar()
menu_roti = "Tava Roti 25 ₹/piece"
menu_dal = "Dal 80 ₹/bowl"
menu_makhani = "Dal Makhni 110 ₹/bowl"
menu_naan = "Naan 50 ₹/piece"
menu_manchurian = "Manchurian 110 ₹/plate"
menu_shahi_panneer = "Shahi paneer 110₹/bowl"
menu_kadahi_paneer = "Kadhai paneer 150/bowl"
menu_masala_gobhi = "Masala gobhi 130₹/bowl"
menu_allo_gobhi = "Aloo gobhi 120₹/bowl"
menu_matar_paneer = "Matar paneer 135₹/bowl"
menu_masala_bhindi = "Masala bhindi 110₹/bowl"
menu_chole = "Chole 100₹/bowl"
menu_rajma = "Rajama 150₹/bowl"
menu_chaap = "Chaap 125₹/bowl"
menu_aloo_parntha = "Aloo parantha 35₹/peice"
menu_cheele = "Cheele 55₹/peice "
listItems = [menu_roti,menu_dal,menu_makhani, menu_naan, menu_manchurian, menu_shahi_panneer,
menu_kadahi_paneer, menu_masala_gobhi, menu_allo_gobhi, menu_matar_paneer, menu_masala_bhindi, menu_chole, menu_rajma, menu_chaap, menu_aloo_parntha, menu_cheele]
Title = Label(frame2, text = " Food Items Prices Quantities", fg = 'red', bg = 'wheat', font=("arial", 30))
Title.grid()
for item in listItems:
label = Label(frame2,text=item, fg = 'yellow', bg = 'wheat', font=("arial", 30))
label.grid(column=0, row=listItems.index(item)+1)
q_roti = Entry(frame2, font=("arial",20), textvariable = roti, fg="Black", width=10)
q_roti.grid(column = 1, row = 1)
q_dal = Entry(frame2, font=("arial",20), textvariable = dal, fg="black", width=10)
q_dal.grid(column = 1, row = 2)
q_makhani = Entry(frame2, font=("arial",20), textvariable = makhani, fg="black", width=10)
q_makhani.grid(column = 1, row = 3)
q_naan = Entry(frame2, font=("arial",20), textvariable = naan, fg="black", width=10)
q_naan.grid(column = 1, row = 4)
q_manchurian = Entry(frame2,font=("arial",20), textvariable = manchurian, fg="black", width=10)
q_manchurian.grid(column = 1, row = 5)
q_shahi_panneer = Entry(frame2, font=("arial",20), textvariable = shahi_panneer, fg="black", width=10)
q_shahi_panneer.grid(column = 1, row = 6)
q_kadahi_panneer = Entry(frame2, font=("arial",20), textvariable = kadahi_paneer, fg="black", width=10)
q_kadahi_panneer.grid(column = 1, row = 7)
q_masala_gobhi = Entry(frame2, font=("arial",20), textvariable = masala_gobhi, fg="black", width=10)
q_masala_gobhi.grid(column = 1, row = 8)
q_allo_gobhi = Entry(frame2, font=("arial",20), textvariable = allo_gobhi, fg="black", width=10)
q_allo_gobhi.grid(column = 1, row = 9)
q_matar_panneer = Entry(frame2, font=("arial",20), textvariable = matar_paneer, fg="black", width=10)
q_matar_panneer.grid(column = 1, row = 10)
q_masala_bhindi = Entry(frame2, font=("arial",20), textvariable = masala_bhindi, fg="black", width=10)
q_masala_bhindi.grid(column = 1, row = 11)
q_cholle = Entry(frame2,font=("arial",20), textvariable = chole, fg="black", width=10)
q_cholle.grid(column = 1, row = 12)
q_rajma = Entry(frame2,font=("arial",20), textvariable = rajma, fg="black", width=10)
q_rajma.grid(column = 1, row = 13)
frame2.update() # update frame2 height so it's no longer 0 ( height is 0 when it has just been created )
canvas_container.configure(yscrollcommand=scrollbar_tk.set, scrollregion="0 0 0 %s" % frame2.winfo_height()) # the scrollregion mustbe the size of the frame inside it,
#in this case "x=0 y=0 width=0 height=frame2height"
#width 0 because we only scroll verticaly so don't mind about the width.
canvas_container.grid(column = 1, row = 0)
scrollbar_tk.grid(column = 0, row = 0, sticky='ns')
frame_container.grid()#.pack(expand=True, fill='both')
root.mainloop()

How can I import this entry answer into a text after

Okay so I have this sign up form and there is a part where you have to enter your name, I want that name answer to be taken to the page after.
import tkinter as tk
root = tk.Tk()
root.geometry("150x50+680+350")
def FormSubmission():
global button_start
button_start.place_forget()
l1.place_forget()
root.attributes("-fullscreen", True)
frame = tk.Frame(root)
tk.Label(frame, text="First Name:").grid(row=0)
name = entry1 = tk.Entry(frame) # I want the name written here to be taken from here to the welcome text.
entry1.grid(row=0, column=1)
tk.Label(frame, text="Last Name:").grid(row=1)
e2 = tk.Entry(frame)
e2.grid(row=1, column=1)
tk.Label(frame, text="Email:").grid(row=2)
e3 = tk.Entry(frame)
e3.grid(row=2, column=1)
tk.Label(frame, text="Date of Birth:").grid(row=3)
e4 = tk.Entry(frame)
e4.grid(row=3, column=1)
frame.pack(anchor='center', expand=True)
button_next = tk.Button(frame, text = "Next", height = 2, width = 7, command =lambda: MainPage(frame))
button_next.grid(row=4, column=1)
def MainPage(frame):
global FormSubmission
frame.pack_forget()
root.attributes("-fullscreen", True)
l1.place(x = 500, y = 10)
button_start.place_forget()
l1 = tk.Label(root, text="Welcome," , font=("Arial", 44)) #As you can see here in this line I want the entry 1 name here after welcome and the comma
button_start = tk.Button(root, text="Start", height=3, width=20, command = FormSubmission)
button_start.place(x = 0, y = 10)
button_exit = tk.Button(root, text="Exit", command=root.destroy)
button_exit.place(x=1506, y=0)
root.mainloop()
What I want to do is take the entry 1 answer and put it in the welcome text. There is an indicator on the lines I'm talking about.
Here is an example how to provide text from your widget entry1
in function FormSubmission():where you are defining your button you should pass the text you want to show in your label
button_next = tk.Button(frame, text = "Next", height = 2, width = 7, command =lambda: MainPage(frame, entry1.get()))
in funtion MainPage(frame):you should set text to your label:
def MainPage(frame, entry1):
global FormSubmission
frame.pack_forget()
root.attributes("-fullscreen", True)
l1.place(x = 500, y = 10)
button_start.place_forget()
l1.config(text="Welcome," + entry1) #<-------

Tkinter Simple Calculation GUI

PICTURE
Hi, I am making a specific calculator(which, does only the same function)
I am trying to make it as similar to the picture shown as possible
This is what I have done so far:
from tkinter import *
root = Tk()
root.geometry('250x150')
firstLabel = Label(root, text='Enter the property value: $').grid(row=1, column=3)
Input = Entry(root).grid(row=1, column=4, padx=5, pady=5)
secondLabel = Label(root, text='Assessment Value:').grid(row=2, column=3)
thirdLabel = Label(root,text='Property Tax:').grid(row=3, column=3)
Button1 = Button(root, text='Calculate').grid(row=4, padx=5, pady=5, column=4)
root.mainloop()
I need you assistance in how to complete it
You have to add command = lambda:function() in your button.
I created a Reverse Polish calculator. The logic will be same you can use this code for reference.
from tkinter import *
import math
import operator
class myapp(Frame):
def __init__ (self,parent):
Frame.__init__(self ,parent)
self.parent=parent
self.initUI()
def initUI(self):
self.parent.title("REVERSE POLISH CALCULATOR")
self.pack( fill=BOTH,expand=True)
frame1 = Frame(self)
frame1.place(x=20,y=30)
frame2 = Frame(self)
frame2.place(x=550,y=45)
frame3 = Frame(self)
frame3.place(x=20,y=100)
frame4 = Frame(self)
frame4.place(x=155,y=113)
frame5 = Frame(self)
frame5.place(x=255,y=213)
operator_list = {'+' : operator.add,
'-' : operator.sub,
'/' : operator.truediv,
'*' : operator.mul,
'^' : operator.pow,
}
def rpn():
cop=0
cnum=0
global res
stack = []
expression=w.get("1.0","end-1c")
labele1=Label(frame5,text="Enter valid Expression")
for i in expression.split(' '):
if i in operator_list:
cop+=1
op1=float(stack.pop())
op2=float(stack.pop())
result=operator_list[i](op1,op2)
stack.append(result)
else:
cnum+=1
stack.append((i))
res=stack.pop()
w2.configure(state="normal")
w2.insert(END,res)
w2.configure(state="disabled")
is_valid=[True if len(expression.split(' '))>2 and cnum==cop+1 else False]
print(is_valid)
if not is_valid:
labele1.pack()
labele1.config(text='yoooo',width=20)
if is_valid :
labele1.pack()
labele1.config(text='wrrroo',width=20)
label = Label( frame1 , text = "EXPRESSION : ",width = 14 , font = "Verdana 10 bold")
label.pack( side = LEFT ,padx = 0 , pady = 15 )
w = Text(frame1,width=35,height=1 ,font=("Helvetica",15))
#w.tag_configure('bold_italics', font=('Arial', 12, 'bold', 'italic'))
w.pack(side="left",fill=X)
#w.bind("<Return>", lambda event: callback(event,frame1))
w.pack(side = "left" , fill = X ,padx = 5 , pady = 15 )
button=Button(frame2 , width = 10 , text = "Calculate" ,font=("Arial",10,"bold"), command = lambda:rpn())
button.pack()
label = Label( frame3 , text = "RESULT : ",width = 10 , font = "Verdana 10 bold")
label.pack( side = LEFT ,padx = 0 , pady = 15 )
w2 = Text(frame4,width=35,height=1 ,font=("Helvetica",15))
w2.configure(state="disabled")
#w.tag_configure('bold_italics', font=('Arial', 12, 'bold', 'italic'))
w2.pack(side="left",fill=X)
#result = rpn(expression)
#print(result)
def main():
root = Tk()
root.geometry("700x500")
#root.resizable(width=False, height=False)
app = myapp(root)
root.mainloop()
if __name__ == '__main__':
main()

root = Tk(), are there any alternatives?

does anyone know any alternatives for:
root = tk()
when i run my program, multiple windows run at the same time, just wondering whether there is an alternative so that they both dont open at the same time. The second window that opens as soon as the program is run should be launched when a button on the initial window is pressed. I also tried using:
root = Toplevel()
but the results are the same.
any ideas? thanks
def Search_Inventory():
#---First Window----
root = Toplevel()
root.title("Warehouse Inventory Control System")
root.configure(bg = "black")
#----Title displayed under the company logo on the first window----
Title_Screen = Label(root,
text="Warehouse Inventory Control System",
fg="grey",
bg="black",
font="Helevetica 25 bold",
pady = "50",
padx = "50",
).grid(row=3, column=4)
#----Interactive Input Boxes for the User----
#----Label to Notify what is needed in the entry box----
PN_Info_Label = Label(root,
text = "Part Number:",
fg="white",
bg="black",
font="Helevetica 15 bold",
padx = 50,
).grid(row=14, column=3, rowspan = 2)
#----Input Box for Part Number
PN_Display = StringVar()
Input_PartNumber = Entry(root,
textvariable=PN_Display,
fg = "blue",
font = "Helevtica 25 bold",
).grid(row=14, column=4)
#----A button that will proceed to the next part of the program----
Search_Button = Button(root,
text = "Search Inventory",
fg = "Blue",
bg = "Grey",
bd = 2,
font="Helevetica 15 bold",
command=lambda:Search_Prod(PN_Display.get()),
height = 1,
width = 15,
).grid(row=16, column=4,pady = 25,padx = 25,)
#----Information regarding how to enter the part number---
Info = Label(root,
text="Please Use Capitals to Enter Part Number",
fg= "red",
bg = "black",
font = "helevetica 12 bold",
).grid(row = 15, column = 4)
#----Adding the company logo to the first window----
photo = PhotoImage(file="image.gif")
photoLabel = Label(image=photo)
photoLabel.image = photo
photoLabel.grid(row=1, column=4, pady = 10)
#----Linking the Help Document----
Help_Button = Button(root,
text = "Help",
command=Help_PDF,
fg = "Red",
bg = "Black",
height = "1",
bd = "2",
font = "Helevetica 20 bold",
width = "4",
).grid(row=0, column=5, pady= 10,padx = 50, sticky = E)
#----Saying who the program was made by----
Credits = Label(root,
text = "Created By: Me",
fg = "White",
bg = "Black",
font = "Helevetica 10 underline",
).grid(row = 19, column = 5)
#To Make Sure that the window doesn't close
root.mainloop()
thats the subroutine that meant to run later but runs straight away.
root_menu = frame()
root_menu.title("Warehouse Inventory Control System")
root_menu.configure(bg = "black")
photo = PhotoImage(file="logo.gif")
photoLabel = Label(image=photo)
photoLabel.image = photo
photoLabel.grid(row=1, column=3,columnspan = 4, sticky = N)
Title_Screen = Label(root_menu,
text="Welcome to the SsangYong\n Warehouse Inventory Control System",
fg="grey",
bg="black",
font="Helevetica 25 bold",
pady = "50",
padx = "50",
).grid(row=2, column=3)
Search_Inventory = Button(root_menu,
command = Search_Inventory(),
text = "Search Inventory",
fg = "Blue",
bg = "Grey",
bd = 2,
font="Helevetica 12 bold",
height = 1,
width = 50,
).grid(row=16, column=3,pady = 25,padx = 25,)
Add_Stock = Button(root_menu,
text = "Add New Items",
fg = "Blue",
bg = "Grey",
bd = 2,
font="Helevetica 12 bold",
height = 1,
width = 60,
).grid(row=15, column=3,pady = 25,padx = 25,)
Print_Report = Button(root_menu,
text = "Print Stock Report",
fg = "Blue",
bg = "Grey",
bd = 2,
font="Helevetica 12 bold",
height = 1,
width = 70,
).grid(row=17, column=3,pady = 25,padx = 25,)
Help_Button = Button(root_menu,
text = "Help",
command=Help_PDF,
fg = "Red",
bg = "Black",
height = "1",
bd = "2",
font = "Helevetica 20 bold",
width = "4",
).grid(row=1, column=3,rowspan = 2, sticky= NE)
Credits = Label(root,
text = "Created By:mk",
fg = "White",
bg = "Black",
font = "Helevetica 10 underline",
).grid(row = 19, column = 5)
root_menu.mainloop()
this is what is meant to pop up initially but comes completely mixed up with the other one.
does anyone know any alternatives for: root = tk() [sic]
There is no alternative. A tkinter application absolutely must have a single root window. If you do not explicitly create one (and you should), one will be created for you the first time you create any other widget.
The second window that opens as soon as the program is run should be launched when a button on the initial window is pressed.
The reason for this is that you are telling it to open immediately. Take a look at this code:
Search_Inventory = Button(root_menu,
command = Search_Inventory(),
The above code is identical to this:
result = Search_Inventory()
Search_Inventory = Button(root_menu, command=result)
Notice the lack of () on the command. The command attribute requires a reference to a function.
command= needs function name - callback - it means without ()
But you have () in
command = Search_Inventory()
and this is why this is executed at start, not when you click button
BTW: tkinter need only one mainloop() - if you use two mainloop() then it works incorrectly.
EDIT: Problem with image is because you didn't set parent for Label in second window
photoLabel = Label(image=photo)
so it used main window as default
You need
photoLabel = Label(root, image=photo)
EDIT: minimal working code
from tkinter import *
def Help_PDF():
pass
def Search_Inventory():
#--- Second Window ----
root = Toplevel()
Title_Screen = Label(root, text="Warehouse Inventory Control System")
Title_Screen.grid(row=3, column=4)
PN_Info_Label = Label(root, text="Part Number:")
PN_Info_Label.grid(row=14, column=3, rowspan=2)
PN_Display = StringVar()
Input_PartNumber = Entry(root, textvariable=PN_Display)
Input_PartNumber.grid(row=14, column=4)
Search_Button = Button(root, text="Search Inventory",
command=lambda:Search_Prod(PN_Display.get()))
Search_Button.grid(row=16, column=4, pady=25, padx=25)
Info = Label(root, text="Please Use Capitals to Enter Part Number")
Info.grid(row=15, column=4)
photo = PhotoImage(file="image.gif")
photoLabel = Label(root, image=photo)
photoLabel.image = photo
photoLabel.grid(row=1, column=4, pady=10)
Help_Button = Button(root, text="Help",
command=Help_PDF)
Help_Button.grid(row=0, column=5, pady=10, padx=50, sticky=E)
Credits = Label(root, text="Created By: Me")
Credits.grid(row=19, column=5)
# --- First Window ---
root_menu = Tk()
photo = PhotoImage(file="logo.gif")
photoLabel = Label(root_menu, image=photo)
photoLabel.image = photo
photoLabel.grid(row=1, column=3, columnspan=4, sticky=N)
Title_Screen = Label(root_menu, text="Welcome to the SsangYong\n Warehouse Inventory Control System",)
Title_Screen.grid(row=2, column=3)
Search_Inventory = Button(root_menu, text="Search Inventory",
command=Search_Inventory)
Search_Inventory.grid(row=16, column=3, pady=25, padx=25)
Add_Stock = Button(root_menu, text="Add New Items")
Add_Stock.grid(row=15, column=3, pady=25, padx=25)
Print_Report = Button(root_menu, text="Print Stock Report")
Print_Report.grid(row=17, column=3, pady=25, padx=25)
Help_Button = Button(root_menu, text="Help",
command=Help_PDF)
Help_Button.grid(row=1, column=3, rowspan=2, sticky=NE)
Credits = Label(root_menu, text="Created By:mk")
Credits.grid(row=19, column=5)
root_menu.mainloop()

Python Tkinter Text/Scrollbar Widget not functioning as expected

I have a homework assignment that I almost have completed, but the scrollbar does not function as expected. While it works when I'm focused on the Text, it does not work when I try to move the scrollbar or use the arrows.
from tkinter import *
class LoanCalculator:
def __init__(self):
window = Tk()
window.title("Loan Calculator")
frame = Frame(window)
frame.pack()
Label(frame, text="Loan Amount").grid(row=1,column=1, sticky=W)
self.loanAmmount = StringVar()
self.entryLoan = Entry(frame, textvariable=self.loanAmmount, justify=RIGHT)
Label(frame, text="Years").grid(row=1, column=3,sticky=W)
self.years = StringVar()
self.entryYears = Entry(frame, textvariable=self.years, justify=RIGHT)
btCalc = Button(frame, text="Calculate Loan", command=self.Calculate)
scrollbar = Scrollbar(frame)
self.text = Text(frame, width=60, height=10,wrap=WORD, yscrollcommand=scrollbar.set)
self.entryLoan.grid(row=1, column=2)
self.entryYears.grid(row=1, column=4)
btCalc.grid(row=1, column=5)
self.text.grid(row=2,column=1,columnspan=4)
scrollbar.grid(row=2, column=5, sticky="NSW")
window.mainloop()
def Calculate(self):
self.text.delete("1.0", END)
self.text.insert(END, "{0:<20s}{1:<20s}{2:<20s}".format("Interest Rate", "Monthly Payment", "Total Payment"))
aIR = 5.0
mIR = 0
mP = 0
tP = 0
fLA = 0
fYear = 0
lA = 0
year = 0
textToOut = ""
while aIR <= 8.0:
fLA = self.loanAmmount.get()
fYear = self.years.get()
lA = int(fLA)
year = int(fYear)
mIR = aIR / 1200
mP = lA * mIR / (1 - (pow(1 / (1 + mIR), year * 12)))
tP = mP * year * 12
textToOut = format(aIR, ">5.3f") + "%" + format(mP, "20.2f") + format(tP, "20.2f") + "\n"
self.text.insert(END, textToOut)
aIR += 1.0 / 8
LoanCalculator()
EDIT
I've changed my question to drop the text portion, as I have figured a workaround for that.
You need to configure the scrollbar to run an action (update the view of the text area) on scrolling. Add this line of code after you defined your text area.
scrollbar.config(command=self.text.yview)
So the code block of __init__ would be something like this:
def __init__(self):
window = Tk()
window.title("Loan Calculator")
frame = Frame(window)
frame.pack()
Label(frame, text="Loan Amount").grid(row=1,column=1, sticky=W)
self.loanAmmount = StringVar()
self.entryLoan = Entry(frame, textvariable=self.loanAmmount, justify=RIGHT)
Label(frame, text="Years").grid(row=1, column=3,sticky=W)
self.years = StringVar()
self.entryYears = Entry(frame, textvariable=self.years, justify=RIGHT)
btCalc = Button(frame, text="Calculate Loan", command=self.Calculate)
scrollbar = Scrollbar(frame)
self.text = Text(frame, width=60, height=10,wrap=WORD, yscrollcommand=scrollbar.set)
scrollbar.config(command=self.text.yview)
self.entryLoan.grid(row=1, column=2)
self.entryYears.grid(row=1, column=4)
btCalc.grid(row=1, column=5)
self.text.grid(row=2,column=1,columnspan=4)
scrollbar.grid(row=2, column=5, sticky="NSW")

Categories