from tkinter import *
master = Tk()
master.title("Add Questions")
master.geometry("800x600")
master.geometry("+275+70")
master.configure(bg="#D3D3D3")
class timer:
def __init__(self,root):
self.root=root
self.text = ""
self.second=10
self.l = Label(root,font=("Calibri (Body)",30),bg="#D3D3D3",fg="#333300")
self.l.place(relx=0.9,rely=0.05,anchor=CENTER)
def update(self):
if self.second>0:
m,s=divmod(self.second,60)
self.text=str(m).zfill(2)+":"+str(s).zfill(2)
self.l.configure(text=self.text)
self.second=self.second-1
self.l.after(1000,self.update)
else:
self.l.configure(text="Time off!",fg="red",font=("Calibri (Body)",70))
self.l.place_configure(relx=0.5,rely=0.5,anchor=CENTER)
self.l.after(1000,lambda: questions(master))
class questions:
def __init__(self,root):
a=timer(master)
a.update()
master.mainloop()
This is my code... class timer: is for countdown. In class questions: I want to destroy self.l label of class timer:. But I don't know how to use self.l attribute in class questions. What should I do?
You can use the .destroy() method.
This makes any widget disappear, and you can't use it again.
So, in your example, this would be self.l.destroy().
As for your class, you can have the second class inherit the first class.
To do that, update your class like this:
class questions(timer):.
And this would be your timer class:
class timer(Object):.
Read up on class inheritance in python.
Hopefully this helps!
Related
I'm making a barcode generator. I need the input in class GUI to be read by class Barcode so it can print the lines on the canvas.
from tkinter import *
class GUI(Frame):
def __init__(self, master=None):
...
self.code_input = Entry(master)
self.code_input.pack()
self.code = self.code_input.get()
...
self.barcode = Barcode(master, height=250, width=200)
self.barcode.pack()
self.barcode.draw()
....
class Barcode(Canvas):
def draw(self):
self.ean = GUI.code
If I reference directly like the above it says AttributeError: type object 'GUI' has no attribute 'code'
If I do inheritance method (based on https://stackoverflow.com/a/19993844/10618936),class Barcode(Canvas, GUI) it says the same as the previous
If I use setter and getter methods:
class GUI(Frame)
def __init__(self, master=None):
...
#property
def code(self):
return self.__code
#code.setter
def code(self, code):
self.__code = code
then self.ean = GUI.code, it won't run the program and say
TypeError: 'property' object is not subscriptable instead
how do I fix this problem? Is the structure of my program really bad? I'm really not good at programming at all... I just want the variable in GUI to be transferred to class Barcode so it can compute and return the result back to GUI to display the barcode
You need to create an instance of the GUI, otherwise you are just referencing to the static class for which code is not defined. You could access it like in this example
class A():
def __init__(self):
self.code = "A"
class B():
def __init__(self):
self.code = "B"
def foo(self):
print(self.code + A().code)
b = B()
b.foo() # Outputs "BA"
Otherwise, to access it like an static variable within the class you need to define it inside the class root level
class A():
code = "C"
def __init__(self):
self.code = "A"
class B():
def __init__(self):
self.code = "B"
def foo(self):
print(self.code + A.code)
b = B()
b.foo() # Outputs "BC"
You should pass the GUI object to the Barcode class, which at the point you create the Barcode instance is self. If you want the Barcode to be inside the GUI frame, you can also directly use it as the Canvas master.
Another thing to notice is that with the way you have it now, self.code will be and remain an empty string, since you only define it right after you've created the Entry widget, at which point it is empty. You should use get on the Entry at the time you want to do something with the contents at that point.
from tkinter import *
class GUI(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.code_input = Entry(self)
self.code_input.pack()
self.barcode = Barcode(self, height=250, width=200)
self.barcode.pack()
Button(self, text="Update Barcode", command=self.barcode.draw).pack()
class Barcode(Canvas):
def __init__(self, master, height, width):
Canvas.__init__(self, master, height=height, width=width)
self.master = master
self.text = self.create_text((100,100))
def draw(self):
self.itemconfig(self.text, text=self.master.code_input.get())
root = Tk()
gui = GUI(root)
gui.pack()
root.mainloop()
For illustration purposes I create a text object on the canvas and update that with the current value of the Entry on a Button click.
I am trying to inherit some values from one class to a another one. I am using the function super to inherit. Below is a simplfied version of my problem. Thank you for help.
from tkinter import *
import random
class First(object):
def __init__(self,master):
super(First, self).__init__(master)
def random(self):
self._y = random.randint(11,20)
self._x = random.randint(1,10)
def random2(self):
s = First(root)
s.random()
class Second(Frame,First):
def __init__(self,master):
super(Second, self).__init__(master)
self.grid()
self.menuFrame = Frame(self)
self.create_menu_widgets()
self.menuFrame.grid()
def create_menu_widgets(self):
btnMainMenu = Button(self.menuFrame,font=("consolas",18,"bold"),text="Main Menu")
btnMainMenu.pack()
def print(self):
print(self._y,self._x)
root = Tk()
x = Second(root)
x.random()
x.random2()
x.print()
root.configure(background = 'green')
root.mainloop()
I keep on getting the error:
super(First, self).__init__(master)
TypeError: object.__init__() takes no parameters
Please help me, I think the problem is where I have s=First(root). Thanks for help.
When you call super on a class that is the highest in your hierarchy it will go object. object is the super class of all objects in Python. So super(First, self).__init__(master) will try to initialize the object not any of your classes. You can see this inheritance using the Class.__mro__. To figure out what I'm talking about.
And inheriting from object? That happens by default even if you don't specify anything. So I guess you wanted to inherit from Frame as object doesn't make any sense.
So change your code to this and it should be fixed.
from tkinter import *
import random
class First(Frame): # changed here
def random(self):
self._y = random.randint(11,20)
self._x = random.randint(1,10)
def random2(self):
s = First(root)
s.random()
class Second(First): # changed here
def __init__(self,master):
super(Second, self).__init__(master)
self.grid()
self.menuFrame = Frame(self)
self.create_menu_widgets()
self.menuFrame.grid()
def create_menu_widgets(self):
btnMainMenu = Button(self.menuFrame,font=("consolas",18,"bold"),text="Main Menu")
btnMainMenu.pack()
def print(self):
print(self._y,self._x)
root = Tk()
x = Second(root)
x.random()
x.random2()
x.print()
root.configure(background = 'green') # you cannot see this as your button fills everything
root.mainloop()
I see several issues in your example.
1:
you are assigning Second() to x but then calling x.random() and x.random2(). This will not work as your random methods only exist in the First() class.
2:
Don't name a function, method, variable or attribute the same thing as a built in method. This will cause problems.
Change your def print(self) to something like def my_print(self) or anything that is not exactly print. While we are talking about this print statement you only define self._x and self._y in your First() class but try to print them in your Second() class. This will never work. self is always a reference to the class object and never a reference to a class controller that was passed to the class.
Now I get what you are trying to do here and I will rebuild your code to show how to share information between classes.
You should not use a geometry manager fro inside the Frame class. Instead use it on the class variable name. This will allow you chose between any geometry manager for the class instead of sticking to just one kind.
As Vineeth pointed out you do not use supper for an object class.
The below code will run the Second() class and then when you want to reference the random methods on the First() class you can do so with the new methods I added to Second(). Let me know if you have any questions.
One last change is to import tkinter as tk this will help prevent accidentally overwriting an imported method from tkinter.
Here is a working example of your code:
import tkinter as tk
import random
class First(object):
def random(self):
return "From First.Random!", random.randint(11,20), random.randint(1,10)
def random2(self):
return "From First.Random2!", self.random()
class Second(tk.Frame):
def __init__(self, master):
super(Second, self).__init__(master)
self.menuFrame = tk.Frame(self)
self.menuFrame.grid()
tk.Button(self.menuFrame, font=("consolas", 18, "bold"), text="Main Menu").pack()
def random(self):
print(First().random())
def random2(self):
print(First().random2())
root = tk.Tk()
root.configure(background='green')
x = Second(root)
x.pack()
x.random()
x.random2()
root.mainloop()
I have a confusion accessing the methods from one class to the other. I want to write on tkinter text widget on class A from inside the method of class B. How am I supposed to do it?
from abc import xyz
from Tkinter import *
class A(Frame):
def write(self,text):
self.display.insert(END,text+'\n')
def __init__(self,parent):
Frame.__init__(self,parent)
self.parent=parent
self.initUI()
def initUI(self):
self.grid(row=0,sticky=N+E+S+W)
self.columnconfigure(0,weight=1)
self.rowconfigure(0,weight=1)
self.display = Text(self)
self.display.grid(row=0,sticky=N+E+S+W)
self.yscroll = Scrollbar(self,command=self.display.yview)
self.yscroll.grid(row=0,column=1,sticky=N+S)
self.display.config(yscrollcommand=self.yscroll.set)
class B(xyz):
def dataReceived(self):
data = 'hello world'
get = A()
get.write(data + '\n')
def main():
root = Tk()
ex = App(root)
root.mainloop()
This gives me error as
get = A()
exceptions.TypeError: __init__() takes exactly 2 arguments (1 given)
Please suggest me the way to inherit the properties of class A in Class B. Thank You for taking time to read this.
If you do not wish to pass a parent, in class A change __init__() to :
def __init__(self,parent=None):
Your problem isn't communicating between two classes, your problem is that you forgot to give the A class an argument. Note the parent argument:
def __init__(self,parent):
Frame.__init__(self,parent)
self.parent=parent
self.initUI()
You have to set the units (text and parent) when you first make the class. So it would be get = A(data + "/n", "some str"
The you could run the get.function without giving any parameters.
Also I'm not sure of you are aware but you are adding two new lines to data not just one
I have two classes here and I want the label from the parent class to be available in the child class. I've tried making the label a global variable but that doesn't seem to work either unless I'm doing something wrong (I've only been using Python for two weeks). Here's the code.
from tkinter import *
class Parent_Class(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.grid()
self.create_labels()
def create_labels(self):
self.label_1 = Label(self)
self.label_1["text"] = "I'm in the Parent Class."
self.label_1.grid(row = 0, column = 0, sticky = W)
class Child_Class():
def __init__(self):
self.change_label()
def change_label(self):
label_1["text"] = "I'm in the Child Class."
window = Tk()
window.title("Label Test")
window.geometry("250x250")
app = Parent_Class(window)
window.mainloop()
You should never directly change a widget outside of a class that creates it. If you choose to modify that original class, you end up having to change two or more classes. Instead, you give the class with the label an interface that other classes can use. You can directly access the label, but it results in what is called "tight coupling"
Regardless, the key to making it work is that you have to pass to the child a reference to the parent.
It works something like this, using an interface. Again, you could directly access parent.label instead of calling an interface, but that's generally a bad idea.
class Parent_Class(...):
def __init__(self, ...):
...
self.label = Label(...)
...
def set_label(self, string):
self.label.configure(text=string)
class Child_Class(...):
def __init__(self, parent):
...
self.parent = parent
...
def change_label(self):
self.parent.set_label("I'm in the Child Class")
# of, if tight coupling is acceptable to you:
# self.parent.label.configure(text="I'm in the Child Class")
...
p = Parent_Class(...)
c = Child_Class(parent=p)
I have the app with Tkinter, for example:
from Tkinter import *
from ttk import *
class MyMenu(Menu):
....
class MyNotebook(Notebook):
....
tk=Tk()
f1=Frame(master=tk)
f2=Frame(master=tk)
menu=MyMenu(master=f1)
notebook=MyNotebook(master=f2)
I want to add command in menu, which will add new tab in notebook. How can i do this?
P.S. f1 != f2 It's important!
P.P.S. functions, that used as commands in menu may be in another file
One of the frames is not necessary for the menu, since it should be configured with the window and not placed with the geometry manager. Something similar to this can do the job:
# ...
def add_tab():
text = "Tab {}".format(len(notebook.tabs()))
frame = Frame(notebook, width=100, height=100)
notebook.add(frame, text=text)
menu=MyMenu()
menu.add_command(label="Add tab", command=add_tab)
tk.config(menu=menu)
However, I recommend you to: a) Define a class instead of using global variables; and b) Don't use import * since Tkinter an ttk uses the same name for different classes. It will be not only more organized, but also easier to read:
import Tkinter as tk
import ttk
class MyMenu(tk.Menu):
pass
class MyNotebook(ttk.Notebook):
pass
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.frame = ttk.Frame(self)
self.notebook = MyNotebook(self.frame)
self.frame.pack()
self.notebook.pack()
self.add_tab()
menu = MyMenu()
menu.add_command(label="Add tab", command=self.add_tab)
self.config(menu=menu)
def add_tab(self):
text = "Tab {}".format(len(self.notebook.tabs()))
frame = ttk.Frame(self.notebook, width=100, height=100)
self.notebook.add(frame, text=text)
app = App()
app.mainloop()
The solution is simple: for an instance of class A to interact with an instance of class B, class A needs a reference to the instance of class B. That means you need to either pass it in to the constructor, or set if after creation. For example:
class MyMenu(Menu):
def __init__(self, notebook):
...
self.add_command("New page", command=notebook.add(...))
...
notebook = Notebook(...)
menu = MyMenu(notebook)
Another way -- which I think is better -- is to pass what is sometimes called a controller -- a class that knows about all the widgets, or provides an interface to the widgets. For example, you could implement your app as a class and use an instance of that as your controller:
class MyMenu(Menu)
def __init__(self, app=None):
...
self.add_command(..., command=app.add_tab)
class App(Tk):
def __init__(self):
...
self.menu = MyMenu(self, controller=self)
self.notebook = Notebook(...)
...
def add_tab(self, label):
frame = Frame(self)
self.notebook.add(frame, text=label)
app = App()
app.mainloop()