I'm having problem with a function that I'm working on. The goal i'm trying to achieve is that, when a number is:
I have a Progress Bar, with the max limit being 1462.
Whenever a number is entered into the window, and the Submit button is pressed, I just simply want the Progress Bar to increase by how much was entered into the window.
I'm getting an error on my increase_progress function, and not being able to achieve the desired result.
Relevant code:
class Window(Frame):
def __init__(self, master):
super().__init__(master)
master.title('Learning Python')
master.configure(background='black')
master.geometry('900x200')
master.resizable(0, 0)
class Submit(Button):
def __init__(self, master):
super().__init__(master)
self.configure(text='Submit', background='black', foreground='light green', highlightthickness=0,
border=0)
class Clear(Button):
def __init__(self, master):
super().__init__(master)
self.configure(text='Clear', background='black', foreground='light green', highlightthickness=0,
border=0)
class ProgressBar(ttk.Progressbar):
def __init__(self, master):
super().__init__(master)
self.config(orient='horizontal', maximum=1462, mode='determinate')
class PagesRead(Label):
def __init__(self, master):
super().__init__(master)
self.config(text='How many page(s) did you read?', background='black', foreground='light green')
class EntryBox(Entry):
def __init__(self, master):
super().__init__(master)
def increase_progress():
progress = int(entry.get())
if progress:
ProgressBar.step(progress)
root = tk.Tk()
app = Window(root)
bar = ProgressBar(root)
bar.pack(fill=tk.BOTH)
pages = PagesRead(root)
pages.pack()
entry = EntryBox(root)
entry.pack()
submit = Submit(root)
submit.pack()
clear = Clear(root)
clear.config(command=lambda: entry.delete(0, 'end'))
clear.pack()
app.mainloop()
import tkinter as tk
import tkinter.ttk as ttk
class Window(tk.Frame):
def __init__(self, master):
super().__init__(master)
master.title('Learning Python')
master.configure(background='black')
master.geometry('900x200')
master.resizable(0, 0)
class Submit(tk.Button):
def __init__(self, master):
super().__init__(master)
self.configure(text='Submit', background='black', foreground='light green', highlightthickness=0,
border=0, command=increase_progress)
class Clear(tk.Button):
def __init__(self, master):
super().__init__(master)
self.configure(text='Clear', background='black', foreground='light green', highlightthickness=0,
border=0)
class ProgressBar(ttk.Progressbar):
def __init__(self, master):
super().__init__(master)
self.config(orient='horizontal', maximum=1462, mode='determinate')
class PagesRead(tk.Label):
def __init__(self, master):
super().__init__(master)
self.config(text='How many page(s) did you read?', background='black', foreground='light green')
class EntryBox(tk.Entry):
def __init__(self, master):
super().__init__(master)
def increase_progress():
progress = int(entry.get())
if progress:
bar['value'] += progress
root = tk.Tk()
app = Window(root)
bar = ProgressBar(root)
bar.pack(fill=tk.BOTH)
pages = PagesRead(root)
pages.pack()
entry = EntryBox(root)
entry.pack()
submit = Submit(root)
submit.pack()
clear = Clear(root)
clear.config(command=lambda: entry.delete(0, 'end'))
clear.pack()
app.mainloop()
Your problem was that you haven't used the value method in progressbar. This code works :) check it out
Related
class SettingsFrame(tk.Frame):
"""docstring for SettingsFrame"""
def __init__(self, master):
self.master = master
self = tk.Frame(self.master)
self.url_label = tk.Label(self, text="Product URL")
self.url_label.pack(side="left")
self.url_entry = tk.Entry(self)
self.url_entry.pack(side="left")
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.settings_frame = SettingsFrame(self)
self.settings_frame.pack(side="top")
def main():
root = tk.Tk()
app = Application(master=root)
app.mainloop()
if __name__ == "__main__":
main()
I'm tryining to structure my code as suggested in this answer - https://stackoverflow.com/a/17470842/12355344, but I'm not sure how to correctly pack() this SettingsFrame with label and entry into Application class.
AttributeError: 'SettingsFrame' object has no attribute 'tk'
Please try this way:
import tkinter as tk
class SettingsFrame(tk.Frame):
"""docstring for SettingsFrame"""
def __init__(self, master=None):
super().__init__(master)
self.url_label = tk.Label(self, text="Product URL")
self.url_label.pack(side="left")
self.url_entry = tk.Entry(self)
self.url_entry.pack(side="left")
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.settings_frame = SettingsFrame()
self.settings_frame.pack(side="top")
def main():
root = tk.Tk()
app = Application(master=root)
app.mainloop()
if __name__ == "__main__":
main()
I've removed the following lines:
self.master = master
self = tk.Frame(self.master)
and added(for SettingsFrame):
super().__init__(master)
Communication between objects
The idea is create a Toplevel window from Gui and after Toplevel closed send the data (name) from Toplevel Entry back to Gui
How object app can know whether the toplev object was destroyed?
or with other words
How can object of Gui know that the object of My_Toplevel is closed?
from tkinter import *
font1 = font=("Open Sans Standard",16,"bold")
class My_Toplevel():
def __init__(self, master=None):
self.master = master
self.toplev = Toplevel(master)
self.name = None
self.create_widgets()
def create_widgets(self):
self.entry_name = Entry(self.toplev, font=font1)
self.button_ok = Button(self.toplev, text="Ok", font=font1,
command=self.get_name)
self.entry_name.pack()
self.button_ok.pack()
def get_name(self):
self.name = self.entry_name.get()
self.toplev.destroy()
class Gui(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.pack()
self.master = master
self.label_text = Label(self, text="Foo Bar Window", font=font1)
self.label_text.pack()
self.button_toplevel = Button(self, text="Create Toplevel",
command=self.get_toplevel, font=font1)
self.button_toplevel.pack()
def get_toplevel(self):
self.my_top = My_Toplevel(self)
if __name__ == "__main__":
root = Tk()
root.title("Parent")
app = Gui(root)
root.mainloop()
You need to pass the data to the Gui instance before you destroy My_Toplevel. One way to do that is to save the name string as an attribute of the Gui instance since you pass that the master parameter when you call My_Toplevel. For example:
from tkinter import *
font1 = font=("Open Sans Standard",16,"bold")
class My_Toplevel():
def __init__(self, master=None):
self.master = master
self.toplev = Toplevel(master)
self.create_widgets()
def create_widgets(self):
self.entry_name = Entry(self.toplev, font=font1)
self.button_ok = Button(self.toplev, text="Ok", font=font1,
command=self.get_name)
self.entry_name.pack()
self.button_ok.pack()
def get_name(self):
self.master.name_data = self.entry_name.get()
self.toplev.destroy()
class Gui(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.pack()
self.master = master
self.label_text = Label(self, text="Foo Bar Window", font=font1)
self.label_text.pack()
self.button_toplevel = Button(self, text="Create Toplevel",
command=self.get_toplevel, font=font1)
self.button_toplevel.pack()
self.name_data = None
Button(self, text="show name", command=self.show_name).pack()
def show_name(self):
print("Name =", self.name_data)
def get_toplevel(self):
self.my_top = My_Toplevel(self)
if __name__ == "__main__":
root = Tk()
root.title("Parent")
app = Gui(root)
root.mainloop()
Press the "show name" button to print the name string to the console.
If you need to save more than a single string, you could append the name to a list, save it in a dictionary, etc.
If you like, you can call the Gui.show_name method just before the TopLevel window is destroyed:
def get_name(self):
self.master.name_data = self.entry_name.get()
self.master.show_name()
self.toplev.destroy()
I'm able to move data from one class frame to another, unfortunately, I can't update the text in the new frame. I can only pack the new data below the original pack
class LabelApp(tk.Frame):
def __init__(self, master, xy_motion=[-1,-1]):
tk.Frame.__init__(self, master )
self.pack()
print('LapApp: ' + str(xy_motion[0]))
self.master = master
self.xy_motion=xy_motion
self.xlabel = tk.Label(text=self.xy_motion[0])
self.xlabel.pack()
def update_label(self, xy_motion ):
self.xlabel.config(text=xy_motion[0])
the print statement above shows correct data
code that is passing over data
class VideoDisplayApp(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.pack()
self.master = master
self.image_label = tk.Label(master=self.master) # label for the vide.
..
.
.
def motion(self, event):
x, y = event.x, event.y
self.xy_motion = [x,y]
print('{}, {}'.format(x, y))
self.motion=tk.Frame(self.master)
self.app = LabelApp(self.motion, self.xy_motion )
Main window
class MainWindow(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.pack(padx=15, pady=15)
self.master.title("Mob")
self.master.resizable(False, False)
self.master.tk_setPalette(background='#e6e6e6')
frame_video = tk.Frame(self)
frame_video.grid(row=0, column=0)
# frame_video.geometry("200x200")
VideoDisplayApp(frame_video)
#
frame_bottom = tk.Frame(self)
frame_bottom.grid(row=1, column=0)
LabelApp(frame_bottom)
#
frame_left = tk.Frame(self)
frame_left.grid(row=0, column=1)
ClickOnFrame(frame_left)
What i wanted to do is, while typing some words in Entry widget, at the same time changing the characters that are displayed in label widget. Here are the codes:
import tkinter as tk
class App(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.entry = tk.Entry(master=self)
self.entry.pack(side="left")
self.var = tk.StringVar()
self.var.set(self.entry.get)
self.label = tk.Label(master=self)
self.label.pack(side="left")
self.configure_widgets()
self.pack()
def configure_widgets(self):
self.label.configure(textvariable=self.var)
if __name__ == "__main__":
root = tk.Tk()
example = App(master=root)
example.mainloop()
Which parts i should change of the codes? Thank you in advance.
Both Entry and Label accept a variable as a parameter. The entry will set the variable value and the Label will get it.
import tkinter as tk
class App(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.var = tk.StringVar()
self.entry = tk.Entry(master=self, textvariable=self.var)
self.entry.pack(side="left")
self.label = tk.Label(master=self, textvariable=self.var)
self.label.pack(side="left")
self.configure_widgets()
self.pack()
def configure_widgets(self):
self.label.configure(textvariable=self.var)
if __name__ == "__main__":
root = tk.Tk()
example = App(master=root)
example.mainloop()
I need help with tkinter frames handling and calling objects into it. Here's my example :
from tkinter import *
class Main(Tk):
def __init__(self):
Tk.__init__(self)
self.frame_one = Frame(self)
self.frame_two = Frame(self)
self.frame_one.pack()
self.frame_two.pack()
self.someclassobj = SomeClass(self.frame_one)
self.someclassobj.pack()
class SomeClass(Frame):
def __init__(self, master):
Frame.__init__(self)
self.frame = Frame(master)
self.frame.pack()
self.btn = Button(self.frame, text='click', command=self.btn_func)
self.btn.pack()
def btn_func(self):
Main.someclassobjtwo = SomeClassTwo(Main.frame_two)
Main.someclassobjtwo.pack()
class SomeClassTwo(Frame):
def __init__(self, master):
Frame.__init__(self)
self.frame = Frame(master)
self.msg = Label(self.frame, text='some msg')
self.msg.pack()
app = Main()
app.mainloop()
I need to call object of SomeClassTwo at Main.frame_two , but i get AttributeError: type object 'Main' has no attribute 'frame_two'
I really don't know why that happens.