So I've passed a create_text variable to "text1"
text1 = UseCanvas(self.window, "text", width = 100, height = 100, text = "Goodbye")
Using:
self.create_text(20,25 width=100, anchor="center", text =self.text, tags= self.tag1)
after passing it to another class.
How do I edit that text widget? I want it to say "Hello" instead of "Goodbye". I've looked all over and I can do anything I want with it EXCEPT change the text.
You should use the itemconfig method of the canvas to modify the text attribute. You have to give to it an id of one or more canvas items. Here is a small working example that lets you change the text of one text object:
# use 'tkinter' instead of 'Tkinter' if using python 3.x
import Tkinter as tk
class Example(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
self.button = tk.Button(self, text="Change text", command=self.on_change_text)
self.canvas = tk.Canvas(self, width=400, height=400)
self.button.pack(side="top", anchor="w")
self.canvas.pack(side="top", fill="both", expand=True)
# every canvas object gets a unique id, which can be used later
# to change the object.
self.text_id = self.canvas.create_text(10,10, anchor="nw", text="Hello, world")
def on_change_text(self):
self.canvas.itemconfig(self.text_id, text="Goodbye, world")
if __name__ == "__main__":
root = tk.Tk()
view = Example(root)
view.pack(side="top", fill="both", expand=True)
root.mainloop()
You should assign the text to a variable(id):
then use this code edit the text:
canvas.itemconfig(your_text_id, text="New Text")
Related
I'm still in my first week of Python, so I'm sorry if this is an obvious mistake...
I want to style a Frame in Tkinter with rounded corners, therefore I got a base64 encoded image, which I applied using ttk.Style following this answer (Tkinter: How to make a rounded corner text widget?) and that worked fine.
My problem is now adapting this into a Class structured program, there the frame is not showing at all.
I replicated the Problem in the code below.
The first window is showing the problem, and the second one how its supposed to look.
How do I make this work? And could there be any new conflicts coming up when I'm not packing the frame to the main window, but into another Frame?
Thanks in advance!
import tkinter as tk
from tkinter import ttk
borderImageData = '''
R0lGODlhQABAAPcAAHx+fMTCxKSipOTi5JSSlNTS1LSytPTy9IyKjMzKzKyq
rOzq7JyanNza3Ly6vPz6/ISChMTGxKSmpOTm5JSWlNTW1LS2tPT29IyOjMzO
zKyurOzu7JyenNze3Ly+vPz+/OkAKOUA5IEAEn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mvbjnzbgMKBuoA/bKDQgC1F
gW8XKMgQOHABBQsMI76wIIOExo0FZIhM8sKGCQYCYA4cwcCEDSYPLOgg4Oro
uhMEdOB84cCAChReB2ZQYcGGkxsGFGCgGzCFCh1QH5jQIW3xugwSzD4QvIIH
4s/PUgiQYcCG4BkC5P/ObpaBhwreq18nb3Z79+8Dwo9nL9I8evjWsdOX6D59
fPH71Xeef/kFyB93/sln4EP2Ebjegg31B5+CEDLUIH4PVqiQhOABqKFCF6qn
34cHcfjffCQaFOJtGaZYkIkUuljQigXK+CKCE3po40A0trgjjDru+EGPI/6I
Y4co7kikkAMBmaSNSzL5gZNSDjkghkXaaGIBHjwpY4gThJeljFt2WSWYMQpZ
5pguUnClehS4tuMEDARQgH8FBMBBBExGwIGdAxywXAUBKHCZkAIoEEAFp33W
QGl47ZgBAwZEwKigE1SQgAUCUDCXiwtQIIAFCTQwgaCrZeCABAzIleIGHDD/
oIAHGUznmXABGMABT4xpmBYBHGgAKGq1ZbppThgAG8EEAW61KwYMSOBAApdy
pNp/BkhAAQLcEqCTt+ACJW645I5rLrgEeOsTBtwiQIEElRZg61sTNBBethSw
CwEA/Pbr778ABywwABBAgAAG7xpAq6mGUUTdAPZ6YIACsRKAAbvtZqzxxhxn
jDG3ybbKFHf36ZVYpuE5oIGhHMTqcqswvyxzzDS/HDMHEiiggQMLDxCZXh8k
BnEBCQTggAUGGKCB0ktr0PTTTEfttNRQT22ABR4EkEABDXgnGUEn31ZABglE
EEAAWaeN9tpqt832221HEEECW6M3wc+Hga3SBgtMODBABw00UEEBgxdO+OGG
J4744oZzXUEDHQxwN7F5G7QRdXxPoPkAnHfu+eeghw665n1vIKhJBQUEADs=
'''
class Application(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
style = ttk.Style()
style.theme_use('clam')
borderImage = tk.PhotoImage("borderImage", data=borderImageData)
style.element_create("RoundedFrame", "image", borderImage, border=16, sticky="nsew")
style.layout("RoundedFrame", [("RoundedFrame", {"sticky": "nsew"})])
frame = ttk.Frame(self, style = "RoundedFrame", padding = 10)
frame.pack()
entry = tk.Entry(frame, borderwidth = 0, width = 40)
entry.pack(fill="both", expand=True)
app = Application()
app.mainloop()
root = tk.Tk()
style = ttk.Style()
style.theme_use('clam')
borderImage = tk.PhotoImage("borderImage", data=borderImageData)
style.element_create("RoundedFrame", "image", borderImage, border=16, sticky="nsew")
style.layout("RoundedFrame", [("RoundedFrame", {"sticky": "nsew"})])
frame = ttk.Frame(root, style = "RoundedFrame", padding = 10)
frame.pack()
entry = tk.Entry(frame, borderwidth = 0, width = 40)
entry.pack(fill="both", expand=True)
root.mainloop()
The problem is pythons garbage collections destroying the PhotoImage instance. You need to keep a reference to it (this is also somewhere in the docs). You probably just wanna save most stuff as an attribute anyway:
class Application(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.style = ttk.Style()
self.style.theme_use('clam')
self.borderImage = tk.PhotoImage("borderImage", data=borderImageData)
self.style.element_create("RoundedFrame", "image", self.borderImage, border=16, sticky="nsew")
self.style.layout("RoundedFrame", [("RoundedFrame", {"sticky": "nsew"})])
self.frame = ttk.Frame(self, style="RoundedFrame", padding=10)
self.frame.pack()
self.entry = tk.Entry(self.frame, borderwidth=0, width=40)
self.entry.pack(fill="both", expand=True)
Works for me.
I want to create some simple tkinter python app (like StickyNotes on Windows), i have create the class mainApplication and i do not know how to by just simply triggering the button create another instance of this class which will be displayed pararell to other window (or even multiple windows). I know how to assigned function to pushButton, and other simple stuff but the problem is with this pararell window displaying. Thanks in advance for help.
class mainApplication(Frame):
_ids = count(0)
def __init__(self, parent):
""" """
self.id = next(self._ids)
Frame.__init__(self, parent)
self.parent = parent
self.parent.minsize(width=200,height=100)
self.parent.geometry(('%dx%d+%d+%d' % (200, 100, 1700, 0+self.id*100)))
self.initUI()
def initUI(self):
""" """
self.parent.title("a2l")
self.pack(fill=BOTH, expand=True)
style = Style()
style.configure("TFrame", background="#333")
frame1 = Frame(self, style="TFrame")
frame1.pack(fill=X)
self.lbl0 = Label(frame1, text="api", width=7, background="#333", foreground = "red")
self.lbl0.pack(side=TOP, padx=5, pady=5)
self.closeButton = Button(self, text="new", command = self.createNewInstance)
self.closeButton.pack(side=RIGHT, padx=5, pady=5)
#=======================================================================
# self.generateButton = Button(self, text="GENERATE", command = self.)
# self.generateButton.pack(side=RIGHT, padx=5, pady=5)
#=======================================================================
def createNewInstance(self):
y = mainApplication()
return y
if __name__ == "__main__":
root = Tk()
x = mainApplication(root).pack(side="top", expand=False)
Tk().mainloop()
You shouldn't create more than one Tk() window in one application with tkinter. Instead tkinter provides a widget called Toplevel which can be useful for this kind of thing.
It creates another window which can exist along side the Tk() window and alongside other Toplevel widgets.
You could use this to create a series of persistent windows with whatever text or widgets you wanted on them at any kind of trigger including a Button.
See my example below for a demonstration:
from tkinter import *
class App:
def __init__(self, root):
self.root = root
self.top = [] #list to contain the Toplevel widgets
self.entry = Entry(self.root)
self.button = Button(self.root, text="Create window", command=self.command)
self.entry.pack()
self.button.pack()
def command(self): #called when button is pressed
self.top.append(Toplevel(self.root)) #adds new Toplevel to the list
Label(self.top[len(self.top)-1], text=self.entry.get()).pack() #Adds label equal to the entry widget to the new toplevel
root = Tk()
App(root)
root.mainloop()
this is my code. I can't put label2 to self.main, and I don't know, how to write a generic function code, that would close the child widgets, that can be specified in the arguments.
import tkinter
class mainwin:
def __init__(self):
self.root = tkinter.Tk()
self.main = tkinter.Canvas(self.root, width=200, height=400)
self.main.place(x=0, y=0, relwidth=1, relheight=1)
self.main.config(bg='green')
self.root.mainloop()
class addlabel:
def __init__(self):
self.label2 = tkinter.Label(mainwin.main, height=2, width=50, text='Hello Noob!!')
#can't put on the canvas 'main'
self.label2.place(x=0, y=50)
self.exit_button = tkinter.Button(self.label2, text='Exit')
self.exit.button.bind('<1>', quit_from_widget)
'''
class quit_from_widget:
def __init__(self):
# what code should be written here, to quit any child widget.
'''
mainwin()
addlabel()
You might be able to use:
mylist = parent.winfo_children();
Then use a for loop and destroy() to close them off
The main reason you can't put the label in is because you are calling mainloop() before addLabel. The program loops through the code and doesn't execute addlabel() until you close the mainwin() function.
secondly, you can't do mainw.main. the class has no reference to that function. instead try adding a parent function to your addlabel like so:
class addlabel:
def __init__(self, parent):
self.label2 = tkinter.Label(parent, height=2, width=50, text='Hello Noob!!')
self.label2.place(x=0, y=50)
self.exit_button = tkinter.Button(self.label2, text='Exit')
self.exit_button.bind('<1>', quit)
Then, when you call the function in mainw class (before the self.root.mainloop() line) you would write:
addlabel(self.main)
import tkinter
class mainwin:
def __init__(self):
self.root = tkinter.Tk()
self.main = tkinter.Canvas(self.root, width=200, height=400)
self.main.place(x=0, y=0, relwidth=1, relheight=1)
self.main.config(bg='green')
self.root.mainloop()
class CustomLabel(tkinter.Frame):
def __init__(self, parent):
tkinter.Frame.__init__(self, parent)
self.label = tkinter.Label(self, height=20, width=30, bg='Red', fg='white', text='Hello')
self.exit_button = tkinter.Button(self, command=self.destroy)
# pack these widgets into this frame. You can use grid or
# place, but pack is easiest for such a simple layout
self.exit_button.pack(side="right")
self.label.pack(side="left", fill="both", expand=True)
window = mainwin()
label = CustomLabel(window.main)
nothing happens. gives green background and the child widget is not visible. but when closing, he writes an error:
....
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: can't invoke "frame" command: application has been destroyed
Process finished with exit code 1
Using tkinter, I am trying to display an image inside the border of an entry widget.
I tried to search in Google but I came with no success, someone have an idea how to do that?
There is no feature or attribute to allow an image inside the boundary of a Entry widget. However, you can simulate it pretty easily by putting an image and an entry widget inside a frame, remove the border from the entry widget, and make sure the entry widget and frame have the same background color.
Example:
import Tkinter as tk
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent, background="gray")
frame = tk.Frame(background="white", borderwidth=1, relief="sunken",
highlightthickness=1)
frame.pack(side="top", fill="x", padx=4, pady=4)
entry = tk.Entry(frame, borderwidth=0, highlightthickness=0, background="white")
entry.image = tk.PhotoImage(data=cancelImageData)
imageLabel = tk.Label(frame, image=entry.image)
imageLabel.pack(side="right", fill="y")
entry.pack(side="left", fill="both", expand=True)
cancelImageData = '''
R0lGODlhEAAQAPcAAAAAAAAAMwAAZgAAmQAAzAAA/wArAAArMwArZgArmQArzAAr
/wBVAABVMwBVZgBVmQBVzABV/wCAAACAMwCAZgCAmQCAzACA/wCqAACqMwCqZgCq
mQCqzACq/wDVAADVMwDVZgDVmQDVzADV/wD/AAD/MwD/ZgD/mQD/zAD//zMAADMA
MzMAZjMAmTMAzDMA/zMrADMrMzMrZjMrmTMrzDMr/zNVADNVMzNVZjNVmTNVzDNV
/zOAADOAMzOAZjOAmTOAzDOA/zOqADOqMzOqZjOqmTOqzDOq/zPVADPVMzPVZjPV
mTPVzDPV/zP/ADP/MzP/ZjP/mTP/zDP//2YAAGYAM2YAZmYAmWYAzGYA/2YrAGYr
M2YrZmYrmWYrzGYr/2ZVAGZVM2ZVZmZVmWZVzGZV/2aAAGaAM2aAZmaAmWaAzGaA
/2aqAGaqM2aqZmaqmWaqzGaq/2bVAGbVM2bVZmbVmWbVzGbV/2b/AGb/M2b/Zmb/
mWb/zGb//5kAAJkAM5kAZpkAmZkAzJkA/5krAJkrM5krZpkrmZkrzJkr/5lVAJlV
M5lVZplVmZlVzJlV/5mAAJmAM5mAZpmAmZmAzJmA/5mqAJmqM5mqZpmqmZmqzJmq
/5nVAJnVM5nVZpnVmZnVzJnV/5n/AJn/M5n/Zpn/mZn/zJn//8wAAMwAM8wAZswA
mcwAzMwA/8wrAMwrM8wrZswrmcwrzMwr/8xVAMxVM8xVZsxVmcxVzMxV/8yAAMyA
M8yAZsyAmcyAzMyA/8yqAMyqM8yqZsyqmcyqzMyq/8zVAMzVM8zVZszVmczVzMzV
/8z/AMz/M8z/Zsz/mcz/zMz///8AAP8AM/8AZv8Amf8AzP8A//8rAP8rM/8rZv8r
mf8rzP8r//9VAP9VM/9VZv9Vmf9VzP9V//+AAP+AM/+AZv+Amf+AzP+A//+qAP+q
M/+qZv+qmf+qzP+q///VAP/VM//VZv/Vmf/VzP/V////AP//M///Zv//mf//zP//
/wAAAAAAAAAAAAAAACH5BAEAAPwALAAAAAAQABAAAAiWAPcJHEiwYEFpCBMiNLhP
WjZz4CB+A5dN2sGH2TJm+7ax4kCHEOlx3EgPHEeLDc1loydwokB6G1EJlEYRHMt6
+1hW/IaSpreN+/ThzIYq5kyKGffV07ePpzSeMzl+UypU6aunMhtSdCcwI0t606A2
3PjN3VVXK2NO+/iKIzZp0xB+Q4Xt4re7te4WZSgNVV+EfhkKLhgQADs=
'''
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(fill="both", expand=True)
root.mainloop()
I'm trying to create a fairly simple e-reader, and I've managed to use tkinter to create something akin to one. But what I can't seem to work out is how to create a scrollbar to allow the user to scroll through the text at will. I can get it working in other pieces of coding, but I can't make it work within this program and I can't work out what the problem is. I've put my simple e-reader, without the attempted scrollbar below.
from Tkinter import *
import tkFileDialog
import ScrolledText
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("File dialog")
self.pack(fill=BOTH, expand=1)
menubar = Menu(self.parent)
self.parent.config(menu=menubar)
fileMenu = Menu(menubar)
fileMenu.add_command(label="Open", command=self.onOpen)
menubar.add_cascade(label="File", menu=fileMenu)
self.txt = Text(self)
self.txt.pack(fill=BOTH, expand=1)
def onOpen(self):
ftypes = [('Python files', '*.py'), ('All files', '*')]
dlg = tkFileDialog.Open(self, filetypes = ftypes)
fl = dlg.show()
if fl != '':
text = self.readFile(fl)
self.txt.insert(END, text)
def readFile(self, filename):
f = open(filename, "r")
text = f.read()
return text
self.txt = ScrolledText(self, undo=True)
self.txt['font'] = ('consolas', '12')
self.txt.pack(expand=True, fill='both')
def main():
root = Tk()
ex = Example(root)
root.geometry("300x250+300+300")
root.mainloop()
if __name__ == '__main__':
main()
Adding a scrollbar to a text widget requires you to do two things in addition to creating the actual widgets:
you must set the yscrollcommand attribute of the text widget to point to the set method of the scrollbar
you must set the command attribute of the scrollbar to point to the yview method of the text widget
For example:
self.text.configure(yscrollcommand=self.scrollbar.set)
self.scrollbar.configure(command=self.text.yview)
I've long wondered whether this could really be the answer to your question, but after the discussion in the comments, it seems it is: You just put the code creating the ScrolledText in the wrong place!
Try moving these three lines (that are now outside of the class, causing a NameError for self)
self.txt = ScrolledText(self, undo=True)
self.txt['font'] = ('consolas', '12')
self.txt.pack(expand=True, fill='both')
to where these lines are in your initUI method (replace these lines)
self.txt = Text(self)
self.txt.pack(fill=BOTH, expand=1)
With other words, in your initUI method, instead of creating a Text widget, create a ScrolledText. Also, change import ScrolledText to from ScrolledText import ScrolledText so that ScrolledText is the actual widget, and not the module defining the widget.