canvas not showing text
My canvas shows a grid of rectangles in randomly changing colors, but when I want it to end, it does not show the "Bye Bye!" message I added to the canvas. Why is that?
Even if I comment the lines for i in ...
inside the __init__
(i.e. the for loop that creates the rectangles in the first place) I don't see the text.
The text update is done in the start of "update" function, after max iterations.
when changing to self.canvas.create_text(100,100, text="Bye Bye!")
, it does seem to work... but only when no rectangles are showing
import tkinter as tk
from time import sleep
import random
class GUI:
def __init__(self, rows = 10, cols = 10, iteration_timer = 1):
self.canv_w = 300
self.canv_h = 300
self.cell_w = self.canv_w // cols
self.cell_h = self.canv_h // rows
self.master = tk.Tk()
self.canvas = tk.Canvas(self.master, width = self.canv_w, height = self.canv_h)
self.canvas.pack()
self.rect_dict = {}
self.iteration_timer = iteration_timer
self.iterations = 0
self.MAX_ITER = 10
for r in range(rows):
for c in range(cols):
self.rect_dict[(r*self.cell_h, c*self.cell_w)] = self.canvas.create_rectangle(r *self.cell_h, c *self.cell_w,
(1+r)*self.cell_h, (1+c)*self.cell_w,
fill="blue")
self.master.after(iteration_timer, self.update)
tk.mainloop()
def update(self):
if self.iterations >= self.MAX_ITER:
self.canvas.create_text(0,0,fill="darkblue", font="Times 100 italic bold", text="Bye Bye!")
sleep(2)
self.canvas.delete(tk.ALL)
self.master.destroy()
return
self.iterations += 1
for k in self.rect_dict:
r = self.rect_dict[k]
c = random.choice(['yellow', 'red', 'black', 'blue' , 'white'])
self.canvas.itemconfig(r, fill=c)
# call again
self.master.after(self.iteration_timer, self.update)
def main():
gui = GUI(rows = 20, cols = 20)
if __name__ == "__main__":
main()
python python-3.x canvas tkinter
add a comment |
My canvas shows a grid of rectangles in randomly changing colors, but when I want it to end, it does not show the "Bye Bye!" message I added to the canvas. Why is that?
Even if I comment the lines for i in ...
inside the __init__
(i.e. the for loop that creates the rectangles in the first place) I don't see the text.
The text update is done in the start of "update" function, after max iterations.
when changing to self.canvas.create_text(100,100, text="Bye Bye!")
, it does seem to work... but only when no rectangles are showing
import tkinter as tk
from time import sleep
import random
class GUI:
def __init__(self, rows = 10, cols = 10, iteration_timer = 1):
self.canv_w = 300
self.canv_h = 300
self.cell_w = self.canv_w // cols
self.cell_h = self.canv_h // rows
self.master = tk.Tk()
self.canvas = tk.Canvas(self.master, width = self.canv_w, height = self.canv_h)
self.canvas.pack()
self.rect_dict = {}
self.iteration_timer = iteration_timer
self.iterations = 0
self.MAX_ITER = 10
for r in range(rows):
for c in range(cols):
self.rect_dict[(r*self.cell_h, c*self.cell_w)] = self.canvas.create_rectangle(r *self.cell_h, c *self.cell_w,
(1+r)*self.cell_h, (1+c)*self.cell_w,
fill="blue")
self.master.after(iteration_timer, self.update)
tk.mainloop()
def update(self):
if self.iterations >= self.MAX_ITER:
self.canvas.create_text(0,0,fill="darkblue", font="Times 100 italic bold", text="Bye Bye!")
sleep(2)
self.canvas.delete(tk.ALL)
self.master.destroy()
return
self.iterations += 1
for k in self.rect_dict:
r = self.rect_dict[k]
c = random.choice(['yellow', 'red', 'black', 'blue' , 'white'])
self.canvas.itemconfig(r, fill=c)
# call again
self.master.after(self.iteration_timer, self.update)
def main():
gui = GUI(rows = 20, cols = 20)
if __name__ == "__main__":
main()
python python-3.x canvas tkinter
add a comment |
My canvas shows a grid of rectangles in randomly changing colors, but when I want it to end, it does not show the "Bye Bye!" message I added to the canvas. Why is that?
Even if I comment the lines for i in ...
inside the __init__
(i.e. the for loop that creates the rectangles in the first place) I don't see the text.
The text update is done in the start of "update" function, after max iterations.
when changing to self.canvas.create_text(100,100, text="Bye Bye!")
, it does seem to work... but only when no rectangles are showing
import tkinter as tk
from time import sleep
import random
class GUI:
def __init__(self, rows = 10, cols = 10, iteration_timer = 1):
self.canv_w = 300
self.canv_h = 300
self.cell_w = self.canv_w // cols
self.cell_h = self.canv_h // rows
self.master = tk.Tk()
self.canvas = tk.Canvas(self.master, width = self.canv_w, height = self.canv_h)
self.canvas.pack()
self.rect_dict = {}
self.iteration_timer = iteration_timer
self.iterations = 0
self.MAX_ITER = 10
for r in range(rows):
for c in range(cols):
self.rect_dict[(r*self.cell_h, c*self.cell_w)] = self.canvas.create_rectangle(r *self.cell_h, c *self.cell_w,
(1+r)*self.cell_h, (1+c)*self.cell_w,
fill="blue")
self.master.after(iteration_timer, self.update)
tk.mainloop()
def update(self):
if self.iterations >= self.MAX_ITER:
self.canvas.create_text(0,0,fill="darkblue", font="Times 100 italic bold", text="Bye Bye!")
sleep(2)
self.canvas.delete(tk.ALL)
self.master.destroy()
return
self.iterations += 1
for k in self.rect_dict:
r = self.rect_dict[k]
c = random.choice(['yellow', 'red', 'black', 'blue' , 'white'])
self.canvas.itemconfig(r, fill=c)
# call again
self.master.after(self.iteration_timer, self.update)
def main():
gui = GUI(rows = 20, cols = 20)
if __name__ == "__main__":
main()
python python-3.x canvas tkinter
My canvas shows a grid of rectangles in randomly changing colors, but when I want it to end, it does not show the "Bye Bye!" message I added to the canvas. Why is that?
Even if I comment the lines for i in ...
inside the __init__
(i.e. the for loop that creates the rectangles in the first place) I don't see the text.
The text update is done in the start of "update" function, after max iterations.
when changing to self.canvas.create_text(100,100, text="Bye Bye!")
, it does seem to work... but only when no rectangles are showing
import tkinter as tk
from time import sleep
import random
class GUI:
def __init__(self, rows = 10, cols = 10, iteration_timer = 1):
self.canv_w = 300
self.canv_h = 300
self.cell_w = self.canv_w // cols
self.cell_h = self.canv_h // rows
self.master = tk.Tk()
self.canvas = tk.Canvas(self.master, width = self.canv_w, height = self.canv_h)
self.canvas.pack()
self.rect_dict = {}
self.iteration_timer = iteration_timer
self.iterations = 0
self.MAX_ITER = 10
for r in range(rows):
for c in range(cols):
self.rect_dict[(r*self.cell_h, c*self.cell_w)] = self.canvas.create_rectangle(r *self.cell_h, c *self.cell_w,
(1+r)*self.cell_h, (1+c)*self.cell_w,
fill="blue")
self.master.after(iteration_timer, self.update)
tk.mainloop()
def update(self):
if self.iterations >= self.MAX_ITER:
self.canvas.create_text(0,0,fill="darkblue", font="Times 100 italic bold", text="Bye Bye!")
sleep(2)
self.canvas.delete(tk.ALL)
self.master.destroy()
return
self.iterations += 1
for k in self.rect_dict:
r = self.rect_dict[k]
c = random.choice(['yellow', 'red', 'black', 'blue' , 'white'])
self.canvas.itemconfig(r, fill=c)
# call again
self.master.after(self.iteration_timer, self.update)
def main():
gui = GUI(rows = 20, cols = 20)
if __name__ == "__main__":
main()
python python-3.x canvas tkinter
python python-3.x canvas tkinter
edited Nov 26 '18 at 9:45
CIsForCookies
asked Nov 26 '18 at 9:39
CIsForCookiesCIsForCookies
6,80811648
6,80811648
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
When you use time.sleep
, everything including the Tkinter mainloop halts. The Tkinter mainloop is responsible for updating the UI, so as long as you don't return to the mainloop you won't see any updates to the UI unless you update it manually. Because you destroy the UI right after the sleep
, you never see the updated UI with the text.
There's basically two thing you can do, either update manually before calling sleep
, or changing the sleep
call to after
, which returns to the mainloop and schedules the function you pass to it.
To update manually call self.master.update()
right before sleep
.
To replace the sleep
with after
, just change
sleep(2)
self.canvas.delete(tk.ALL)
self.master.destroy()
to
self.master.after(2000, self.master.destroy)
another question - do you know how can I make the canvas (except for the text) half-transparent?
– CIsForCookies
Nov 26 '18 at 10:03
I don't think you can do transparency on canvas items. Closest thing isstipple
. If you addtags='rect'
to your rectangle creation, you can set the stipple pattern for all rectangles at once withself.canvas.itemconfig('rect', stipple='gray50')
. You can find the stipple options (which are bitmap images) here.
– fhdrsdg
Nov 26 '18 at 11:39
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53478278%2fcanvas-not-showing-text%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
When you use time.sleep
, everything including the Tkinter mainloop halts. The Tkinter mainloop is responsible for updating the UI, so as long as you don't return to the mainloop you won't see any updates to the UI unless you update it manually. Because you destroy the UI right after the sleep
, you never see the updated UI with the text.
There's basically two thing you can do, either update manually before calling sleep
, or changing the sleep
call to after
, which returns to the mainloop and schedules the function you pass to it.
To update manually call self.master.update()
right before sleep
.
To replace the sleep
with after
, just change
sleep(2)
self.canvas.delete(tk.ALL)
self.master.destroy()
to
self.master.after(2000, self.master.destroy)
another question - do you know how can I make the canvas (except for the text) half-transparent?
– CIsForCookies
Nov 26 '18 at 10:03
I don't think you can do transparency on canvas items. Closest thing isstipple
. If you addtags='rect'
to your rectangle creation, you can set the stipple pattern for all rectangles at once withself.canvas.itemconfig('rect', stipple='gray50')
. You can find the stipple options (which are bitmap images) here.
– fhdrsdg
Nov 26 '18 at 11:39
add a comment |
When you use time.sleep
, everything including the Tkinter mainloop halts. The Tkinter mainloop is responsible for updating the UI, so as long as you don't return to the mainloop you won't see any updates to the UI unless you update it manually. Because you destroy the UI right after the sleep
, you never see the updated UI with the text.
There's basically two thing you can do, either update manually before calling sleep
, or changing the sleep
call to after
, which returns to the mainloop and schedules the function you pass to it.
To update manually call self.master.update()
right before sleep
.
To replace the sleep
with after
, just change
sleep(2)
self.canvas.delete(tk.ALL)
self.master.destroy()
to
self.master.after(2000, self.master.destroy)
another question - do you know how can I make the canvas (except for the text) half-transparent?
– CIsForCookies
Nov 26 '18 at 10:03
I don't think you can do transparency on canvas items. Closest thing isstipple
. If you addtags='rect'
to your rectangle creation, you can set the stipple pattern for all rectangles at once withself.canvas.itemconfig('rect', stipple='gray50')
. You can find the stipple options (which are bitmap images) here.
– fhdrsdg
Nov 26 '18 at 11:39
add a comment |
When you use time.sleep
, everything including the Tkinter mainloop halts. The Tkinter mainloop is responsible for updating the UI, so as long as you don't return to the mainloop you won't see any updates to the UI unless you update it manually. Because you destroy the UI right after the sleep
, you never see the updated UI with the text.
There's basically two thing you can do, either update manually before calling sleep
, or changing the sleep
call to after
, which returns to the mainloop and schedules the function you pass to it.
To update manually call self.master.update()
right before sleep
.
To replace the sleep
with after
, just change
sleep(2)
self.canvas.delete(tk.ALL)
self.master.destroy()
to
self.master.after(2000, self.master.destroy)
When you use time.sleep
, everything including the Tkinter mainloop halts. The Tkinter mainloop is responsible for updating the UI, so as long as you don't return to the mainloop you won't see any updates to the UI unless you update it manually. Because you destroy the UI right after the sleep
, you never see the updated UI with the text.
There's basically two thing you can do, either update manually before calling sleep
, or changing the sleep
call to after
, which returns to the mainloop and schedules the function you pass to it.
To update manually call self.master.update()
right before sleep
.
To replace the sleep
with after
, just change
sleep(2)
self.canvas.delete(tk.ALL)
self.master.destroy()
to
self.master.after(2000, self.master.destroy)
answered Nov 26 '18 at 9:56
fhdrsdgfhdrsdg
7,07621837
7,07621837
another question - do you know how can I make the canvas (except for the text) half-transparent?
– CIsForCookies
Nov 26 '18 at 10:03
I don't think you can do transparency on canvas items. Closest thing isstipple
. If you addtags='rect'
to your rectangle creation, you can set the stipple pattern for all rectangles at once withself.canvas.itemconfig('rect', stipple='gray50')
. You can find the stipple options (which are bitmap images) here.
– fhdrsdg
Nov 26 '18 at 11:39
add a comment |
another question - do you know how can I make the canvas (except for the text) half-transparent?
– CIsForCookies
Nov 26 '18 at 10:03
I don't think you can do transparency on canvas items. Closest thing isstipple
. If you addtags='rect'
to your rectangle creation, you can set the stipple pattern for all rectangles at once withself.canvas.itemconfig('rect', stipple='gray50')
. You can find the stipple options (which are bitmap images) here.
– fhdrsdg
Nov 26 '18 at 11:39
another question - do you know how can I make the canvas (except for the text) half-transparent?
– CIsForCookies
Nov 26 '18 at 10:03
another question - do you know how can I make the canvas (except for the text) half-transparent?
– CIsForCookies
Nov 26 '18 at 10:03
I don't think you can do transparency on canvas items. Closest thing is
stipple
. If you add tags='rect'
to your rectangle creation, you can set the stipple pattern for all rectangles at once with self.canvas.itemconfig('rect', stipple='gray50')
. You can find the stipple options (which are bitmap images) here.– fhdrsdg
Nov 26 '18 at 11:39
I don't think you can do transparency on canvas items. Closest thing is
stipple
. If you add tags='rect'
to your rectangle creation, you can set the stipple pattern for all rectangles at once with self.canvas.itemconfig('rect', stipple='gray50')
. You can find the stipple options (which are bitmap images) here.– fhdrsdg
Nov 26 '18 at 11:39
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53478278%2fcanvas-not-showing-text%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown