I am starting to learn OOP and I've been struggling with some basic stuff.
In the code below, I have created a class Scales() that I want to use to create 2 very similar scales, with only their variable option being different.
How can I pass the name of these variables as a parameter when I call Scales() and make both of them a DoubleVar type?
from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk
import numpy as np
class Scales(Frame):
def __init__(self, parent, variable_name, label_text, initial_value,
final_value):
self.parent = parent
self.bar_length = 200
self.variable_name = variable_name
self.label_text = label_text
self.initial_value = initial_value
self.final_value = final_value
# self.variable_name = DoubleVar()
self.scale_name = Scale(self.parent, variable=self.variable_name,
orient=HORIZONTAL,
from_=self.initial_value,
to=self.final_value,
length=self.bar_length, cursor="hand",
label=self.label_text)
class MainApplication(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.slice_number_scale = Scales(self.parent, slice_number,
"Slice Number", 1, 24)
if __name__ == '__main__':
root = Tk()
root.geometry("800x600")
MainApplication(root)
root.mainloop()
If the variables are going to live as instance variables of your Scales class, then there's absolutely no reason to give them separate names; every reference to them is going to be in the context of some particular instance. You'd probably want to define a get() method that does something like return self.variable.get(), for the convenience of the class's user.
If the variables live somewhere outside the class, then Scales should not care what their names are; pass the variable itself as a parameter to the class constructor, and pass it on as the variable= option to Scale().
Just create the variables in each Scale class instance that is created, then access them through the instance's name. Here's what I mean:
from tkinter import *
#from tkinter import ttk
#from PIL import Image, ImageTk
#import numpy as np
class Scale(Frame):
""" Dummy version of class for testing and illustration. """
def __init__(self, parent, orient=None, from_=None, to=None, length=None,
cursor=None, label=None):
Frame.__init__(self, parent) # initialize base class
self.variable = DoubleVar() # create variable and make attribute
class Scales(Frame):
def __init__(self, parent, label_text, initial_value,
final_value):
self.parent = parent
self.bar_length = 200
# self.variable_name = variable_name
self.label_text = label_text
self.initial_value = initial_value
self.final_value = final_value
# self.variable_name = DoubleVar()
self.scale1 = Scale(self.parent,
# variable=self.variable_name,
orient=HORIZONTAL,
from_=self.initial_value,
to=self.final_value,
length=self.bar_length,
cursor="hand",
label=self.label_text)
self.scale1.pack()
class MainApplication(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
slice_number = 42
self.slice_number_scale = Scales(self.parent, slice_number, 1, 24)
root = Tk()
app = MainApplication(root)
app.mainloop()
After doing this you can access the variable for each Scale instance within a Scales instance as self.scale1.variable (and self.scale2.variable after you add it). Within the MainApplication instance they can be referred to as self.slice_number_scale.scale1.variable (and self.slice_number_scale2.variable).
For the latter you might want to add methods to class MainApplication to make such references more succinct, such as:
class MainApplication(Frame):
....
def get_scale_var1(self):
return self.slice_number_scale.scale1.variable.get()
Related
This attempt is try to get the value from a function in class One which is retrieve when the function is call. Then the value [string1] would be passed as a parameter for class Two's method call which's within tkinter, any idea on how can it be attain
from tkinter import Tk, Label, Button
from tkinter import *
from tkinter.ttk import *
class One:
def __init__(self, master):
self.master = master
master.title("Test")
self.greet_button = Button(master, text="Test", command=self.do)
self.greet_button.pack()
def do(self):
...some action...
string1='test'
return string1
class Two:
def write(self, str):
...some action...
#object instantiate within tkinter
root = Tk()
p0 = One(root)
p0.do()
p1 = Two()
p1.write(string1)
root.mainloop()
##from tkinter import Tk, Label, Button
##
##from tkinter import *
##from tkinter.ttk import *
####why are you importing some and then everything?
import tkinter as tk
class Reader_Class(tk.Frame):
def __init__(self, master, writer_class):
tk.Frame.__init__(self,master)
self.master = master
master.title("Test")
self.writer = writer_class
self.greet_button = tk.Button(self, text="Test", command=self.do)
self.greet_button.pack()
def do(self):
string1='test'
self.writer.write(string1)
class Writer_Class(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self,master)
def write(self, test):
print(test)
root = tk.Tk()
writer = Writer_Class(root)
writer.pack()
reader = Reader_Class(root, writer)
reader.pack()
root.mainloop()
Explaination:
So first of all, it isnt necessary to import stuff twice.
Second you need to pass through the interface a reference, therefor you need to have reference. Thats why it makes sense to use the construktor first on the writer_class.
Next you give the reference as a parameter to the reader_class.
reader = Reader_Class(root, writer)
In the reader_class you keep a reference in the local_scope by using the syntax self..
self.writer = writer_class
Finally you use the reference of that class and calling a method of this class, where you can pass through a parameter of your choice.
self.writer.write(string1)
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()
from tkinter import *
class MainBattle(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
global canvas
self.parent.title('Python')
self.pack(fill = BOTH, expand = 1)
canvas = Canvas(self)
self.Label_My = Label(self, text = 'MyObject')
self.Label_My.place(x = 470, y = 35)
canvas.pack(fill = BOTH, expand = 1)
canvas.update()
def aa(self):
self.Label_My['text'] = 'HisObject'
def Change():
Label_My['text'] = 'HisObject'
root = Tk()
ex = MainBattle(root)
root.geometry('700x500')
it should use global method?
I would defind labels inside the class and change it's text outside class if possible.
You don't need global variables. You have a reference to the instance, which allows you to access all instance variables:
ex.Label_My["text"] = "HisObject"
If your question is "can I use global to set variable values from outside the class" then yes.
Whenever you want to change the value of a global variable you need to write global.
def changetext():
global label_text
label_text = "new text"
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)