Resetting generated widget count in Python with Tkinter












0















Using Python 3.7 - Working in Pycharm



I am currently working on a project where I am constantly generating new widgets and removing them to show different things, and I came across a fairly annoying problem that I can't seem to figure out, let alone find information on.
When the top widget(frame) in a row of frames has been made 255 times it crashes. I guess it has something to do with how it stores the information in bytes.



Edit*



I realize that my initial explaination was more complicated than it need to so here is a simplified version:



from tkinter import *

list =
for x in range(1):
e = Frame()
list.append(e)
print(list) #Prints .!frame

list.clear()
print(list) #Prints , the list is now empty

for x in range(1):
e = Frame()
list.append(e)
print(list) #Prints .!frame2, which is one step up from the previous one even the list started empty

list[0].destroy()
list[0].forget()
print(list) #.!frame2 is still there

list.clear()

print(list) #list is now empty again

for x in range(1):
e = Frame()
list.append(e)
print(list) #Prints .!frame3, and it still remembers.


As you can see, it keeps adding to the .!frame number.



What I am looking for is a way to keep it from reaching .!frame255










share|improve this question

























  • Please provide a proper Minimal, Complete, and Verifiable example that shows how you tried destroying the widgets. It's very difficult to understand your problem with just a description of the code.

    – Bryan Oakley
    Nov 14 '18 at 15:57











  • Yes you are right, after re-reading it I realize it did not easily explain my problem. I have not added a clear example as to what is happening and what I want to happen.

    – Rattja
    Nov 15 '18 at 10:45


















0















Using Python 3.7 - Working in Pycharm



I am currently working on a project where I am constantly generating new widgets and removing them to show different things, and I came across a fairly annoying problem that I can't seem to figure out, let alone find information on.
When the top widget(frame) in a row of frames has been made 255 times it crashes. I guess it has something to do with how it stores the information in bytes.



Edit*



I realize that my initial explaination was more complicated than it need to so here is a simplified version:



from tkinter import *

list =
for x in range(1):
e = Frame()
list.append(e)
print(list) #Prints .!frame

list.clear()
print(list) #Prints , the list is now empty

for x in range(1):
e = Frame()
list.append(e)
print(list) #Prints .!frame2, which is one step up from the previous one even the list started empty

list[0].destroy()
list[0].forget()
print(list) #.!frame2 is still there

list.clear()

print(list) #list is now empty again

for x in range(1):
e = Frame()
list.append(e)
print(list) #Prints .!frame3, and it still remembers.


As you can see, it keeps adding to the .!frame number.



What I am looking for is a way to keep it from reaching .!frame255










share|improve this question

























  • Please provide a proper Minimal, Complete, and Verifiable example that shows how you tried destroying the widgets. It's very difficult to understand your problem with just a description of the code.

    – Bryan Oakley
    Nov 14 '18 at 15:57











  • Yes you are right, after re-reading it I realize it did not easily explain my problem. I have not added a clear example as to what is happening and what I want to happen.

    – Rattja
    Nov 15 '18 at 10:45
















0












0








0








Using Python 3.7 - Working in Pycharm



I am currently working on a project where I am constantly generating new widgets and removing them to show different things, and I came across a fairly annoying problem that I can't seem to figure out, let alone find information on.
When the top widget(frame) in a row of frames has been made 255 times it crashes. I guess it has something to do with how it stores the information in bytes.



Edit*



I realize that my initial explaination was more complicated than it need to so here is a simplified version:



from tkinter import *

list =
for x in range(1):
e = Frame()
list.append(e)
print(list) #Prints .!frame

list.clear()
print(list) #Prints , the list is now empty

for x in range(1):
e = Frame()
list.append(e)
print(list) #Prints .!frame2, which is one step up from the previous one even the list started empty

list[0].destroy()
list[0].forget()
print(list) #.!frame2 is still there

list.clear()

print(list) #list is now empty again

for x in range(1):
e = Frame()
list.append(e)
print(list) #Prints .!frame3, and it still remembers.


As you can see, it keeps adding to the .!frame number.



What I am looking for is a way to keep it from reaching .!frame255










share|improve this question
















Using Python 3.7 - Working in Pycharm



I am currently working on a project where I am constantly generating new widgets and removing them to show different things, and I came across a fairly annoying problem that I can't seem to figure out, let alone find information on.
When the top widget(frame) in a row of frames has been made 255 times it crashes. I guess it has something to do with how it stores the information in bytes.



Edit*



I realize that my initial explaination was more complicated than it need to so here is a simplified version:



from tkinter import *

list =
for x in range(1):
e = Frame()
list.append(e)
print(list) #Prints .!frame

list.clear()
print(list) #Prints , the list is now empty

for x in range(1):
e = Frame()
list.append(e)
print(list) #Prints .!frame2, which is one step up from the previous one even the list started empty

list[0].destroy()
list[0].forget()
print(list) #.!frame2 is still there

list.clear()

print(list) #list is now empty again

for x in range(1):
e = Frame()
list.append(e)
print(list) #Prints .!frame3, and it still remembers.


As you can see, it keeps adding to the .!frame number.



What I am looking for is a way to keep it from reaching .!frame255







python list tkinter widget






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 10:44







Rattja

















asked Nov 14 '18 at 13:07









RattjaRattja

92




92













  • Please provide a proper Minimal, Complete, and Verifiable example that shows how you tried destroying the widgets. It's very difficult to understand your problem with just a description of the code.

    – Bryan Oakley
    Nov 14 '18 at 15:57











  • Yes you are right, after re-reading it I realize it did not easily explain my problem. I have not added a clear example as to what is happening and what I want to happen.

    – Rattja
    Nov 15 '18 at 10:45





















  • Please provide a proper Minimal, Complete, and Verifiable example that shows how you tried destroying the widgets. It's very difficult to understand your problem with just a description of the code.

    – Bryan Oakley
    Nov 14 '18 at 15:57











  • Yes you are right, after re-reading it I realize it did not easily explain my problem. I have not added a clear example as to what is happening and what I want to happen.

    – Rattja
    Nov 15 '18 at 10:45



















Please provide a proper Minimal, Complete, and Verifiable example that shows how you tried destroying the widgets. It's very difficult to understand your problem with just a description of the code.

– Bryan Oakley
Nov 14 '18 at 15:57





Please provide a proper Minimal, Complete, and Verifiable example that shows how you tried destroying the widgets. It's very difficult to understand your problem with just a description of the code.

– Bryan Oakley
Nov 14 '18 at 15:57













Yes you are right, after re-reading it I realize it did not easily explain my problem. I have not added a clear example as to what is happening and what I want to happen.

– Rattja
Nov 15 '18 at 10:45







Yes you are right, after re-reading it I realize it did not easily explain my problem. I have not added a clear example as to what is happening and what I want to happen.

– Rattja
Nov 15 '18 at 10:45














1 Answer
1






active

oldest

votes


















0















When the top widget(frame) in a row of frames has been made 255 times it crashes. I guess it has something to do with how it stores the information in bytes.




No, it has nothing to do with that. I think your assertion that the program crashes when the number reaches 255 is likely wrong. It's very easy to create a tkinter program that continues to work even when that number reaches into the thousands. If you look in the tkinter source code you'll see that it's just a plain integer that is appended to a string.





You seem to misunderstand the relationship between your list variable and the widgets that are stored in it. Clearing the list will not destroy the widgets in the list. You must delete each one individually, or destroy their parent. In this case their parent is the root window so that's not a viable solution.



If you want to destroy all of the frames, use a small loop before you clear the list:



for frame in list:
frame.destroy()


The number that tkinter assigns to the widget is inconsequential. It doesn't matter what the number is, and it may or may not reflect how many widgets actually exist. It's an internal detail that is unimportant.



If you want to see how many widgets actually exist, you can call winfo_children on the root window. For that you need a reference to the root window. The easiest and best way to do that is to explicitly create the root window at the start of your code:



root = Tk()


At the end of your script you can print out all of the windows that actually exist:



print("all windows:", root.winfo_children())


In your example code it shows exactly two widgets, which is what is expected. You create a widget, then you create a second widget, then you delete the first widget, and then you create a third widget.






share|improve this answer


























  • That is the thing I was trying to show though. Even if I destroy before clearing it will still keep counting. I tried to show this with the list[0].destroy, which does the same as your for loop (as long as it has just 1 entry ofc). Doing it via a for loop does not change the outcome, just tried.

    – Rattja
    Nov 15 '18 at 14:44











  • "That is the thing I was trying to show though. Even if I destroy before clearing it will still keep counting. " - your code doesn't illustrate that, and what you say contradicts how tkinter works. The fact that it keeps counting is inconsequential -- the number can go to 10000 and still work.

    – Bryan Oakley
    Nov 15 '18 at 14:52











  • I see now, destroying before clearing does fix the problem. I thought it did not work as I could still see it keep the count. Tested it by generating one frame and clearing the list 50 000+ times with Tkinter window open. When it's done, closing the window with the x takes a long time. If I add the destroy() before clearing it's instant. So I guess that if you don't destroy, and keep generating a huge amount of frames over and over, you'll run into memory problems.

    – Rattja
    Nov 15 '18 at 15:52











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300973%2fresetting-generated-widget-count-in-python-with-tkinter%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









0















When the top widget(frame) in a row of frames has been made 255 times it crashes. I guess it has something to do with how it stores the information in bytes.




No, it has nothing to do with that. I think your assertion that the program crashes when the number reaches 255 is likely wrong. It's very easy to create a tkinter program that continues to work even when that number reaches into the thousands. If you look in the tkinter source code you'll see that it's just a plain integer that is appended to a string.





You seem to misunderstand the relationship between your list variable and the widgets that are stored in it. Clearing the list will not destroy the widgets in the list. You must delete each one individually, or destroy their parent. In this case their parent is the root window so that's not a viable solution.



If you want to destroy all of the frames, use a small loop before you clear the list:



for frame in list:
frame.destroy()


The number that tkinter assigns to the widget is inconsequential. It doesn't matter what the number is, and it may or may not reflect how many widgets actually exist. It's an internal detail that is unimportant.



If you want to see how many widgets actually exist, you can call winfo_children on the root window. For that you need a reference to the root window. The easiest and best way to do that is to explicitly create the root window at the start of your code:



root = Tk()


At the end of your script you can print out all of the windows that actually exist:



print("all windows:", root.winfo_children())


In your example code it shows exactly two widgets, which is what is expected. You create a widget, then you create a second widget, then you delete the first widget, and then you create a third widget.






share|improve this answer


























  • That is the thing I was trying to show though. Even if I destroy before clearing it will still keep counting. I tried to show this with the list[0].destroy, which does the same as your for loop (as long as it has just 1 entry ofc). Doing it via a for loop does not change the outcome, just tried.

    – Rattja
    Nov 15 '18 at 14:44











  • "That is the thing I was trying to show though. Even if I destroy before clearing it will still keep counting. " - your code doesn't illustrate that, and what you say contradicts how tkinter works. The fact that it keeps counting is inconsequential -- the number can go to 10000 and still work.

    – Bryan Oakley
    Nov 15 '18 at 14:52











  • I see now, destroying before clearing does fix the problem. I thought it did not work as I could still see it keep the count. Tested it by generating one frame and clearing the list 50 000+ times with Tkinter window open. When it's done, closing the window with the x takes a long time. If I add the destroy() before clearing it's instant. So I guess that if you don't destroy, and keep generating a huge amount of frames over and over, you'll run into memory problems.

    – Rattja
    Nov 15 '18 at 15:52
















0















When the top widget(frame) in a row of frames has been made 255 times it crashes. I guess it has something to do with how it stores the information in bytes.




No, it has nothing to do with that. I think your assertion that the program crashes when the number reaches 255 is likely wrong. It's very easy to create a tkinter program that continues to work even when that number reaches into the thousands. If you look in the tkinter source code you'll see that it's just a plain integer that is appended to a string.





You seem to misunderstand the relationship between your list variable and the widgets that are stored in it. Clearing the list will not destroy the widgets in the list. You must delete each one individually, or destroy their parent. In this case their parent is the root window so that's not a viable solution.



If you want to destroy all of the frames, use a small loop before you clear the list:



for frame in list:
frame.destroy()


The number that tkinter assigns to the widget is inconsequential. It doesn't matter what the number is, and it may or may not reflect how many widgets actually exist. It's an internal detail that is unimportant.



If you want to see how many widgets actually exist, you can call winfo_children on the root window. For that you need a reference to the root window. The easiest and best way to do that is to explicitly create the root window at the start of your code:



root = Tk()


At the end of your script you can print out all of the windows that actually exist:



print("all windows:", root.winfo_children())


In your example code it shows exactly two widgets, which is what is expected. You create a widget, then you create a second widget, then you delete the first widget, and then you create a third widget.






share|improve this answer


























  • That is the thing I was trying to show though. Even if I destroy before clearing it will still keep counting. I tried to show this with the list[0].destroy, which does the same as your for loop (as long as it has just 1 entry ofc). Doing it via a for loop does not change the outcome, just tried.

    – Rattja
    Nov 15 '18 at 14:44











  • "That is the thing I was trying to show though. Even if I destroy before clearing it will still keep counting. " - your code doesn't illustrate that, and what you say contradicts how tkinter works. The fact that it keeps counting is inconsequential -- the number can go to 10000 and still work.

    – Bryan Oakley
    Nov 15 '18 at 14:52











  • I see now, destroying before clearing does fix the problem. I thought it did not work as I could still see it keep the count. Tested it by generating one frame and clearing the list 50 000+ times with Tkinter window open. When it's done, closing the window with the x takes a long time. If I add the destroy() before clearing it's instant. So I guess that if you don't destroy, and keep generating a huge amount of frames over and over, you'll run into memory problems.

    – Rattja
    Nov 15 '18 at 15:52














0












0








0








When the top widget(frame) in a row of frames has been made 255 times it crashes. I guess it has something to do with how it stores the information in bytes.




No, it has nothing to do with that. I think your assertion that the program crashes when the number reaches 255 is likely wrong. It's very easy to create a tkinter program that continues to work even when that number reaches into the thousands. If you look in the tkinter source code you'll see that it's just a plain integer that is appended to a string.





You seem to misunderstand the relationship between your list variable and the widgets that are stored in it. Clearing the list will not destroy the widgets in the list. You must delete each one individually, or destroy their parent. In this case their parent is the root window so that's not a viable solution.



If you want to destroy all of the frames, use a small loop before you clear the list:



for frame in list:
frame.destroy()


The number that tkinter assigns to the widget is inconsequential. It doesn't matter what the number is, and it may or may not reflect how many widgets actually exist. It's an internal detail that is unimportant.



If you want to see how many widgets actually exist, you can call winfo_children on the root window. For that you need a reference to the root window. The easiest and best way to do that is to explicitly create the root window at the start of your code:



root = Tk()


At the end of your script you can print out all of the windows that actually exist:



print("all windows:", root.winfo_children())


In your example code it shows exactly two widgets, which is what is expected. You create a widget, then you create a second widget, then you delete the first widget, and then you create a third widget.






share|improve this answer
















When the top widget(frame) in a row of frames has been made 255 times it crashes. I guess it has something to do with how it stores the information in bytes.




No, it has nothing to do with that. I think your assertion that the program crashes when the number reaches 255 is likely wrong. It's very easy to create a tkinter program that continues to work even when that number reaches into the thousands. If you look in the tkinter source code you'll see that it's just a plain integer that is appended to a string.





You seem to misunderstand the relationship between your list variable and the widgets that are stored in it. Clearing the list will not destroy the widgets in the list. You must delete each one individually, or destroy their parent. In this case their parent is the root window so that's not a viable solution.



If you want to destroy all of the frames, use a small loop before you clear the list:



for frame in list:
frame.destroy()


The number that tkinter assigns to the widget is inconsequential. It doesn't matter what the number is, and it may or may not reflect how many widgets actually exist. It's an internal detail that is unimportant.



If you want to see how many widgets actually exist, you can call winfo_children on the root window. For that you need a reference to the root window. The easiest and best way to do that is to explicitly create the root window at the start of your code:



root = Tk()


At the end of your script you can print out all of the windows that actually exist:



print("all windows:", root.winfo_children())


In your example code it shows exactly two widgets, which is what is expected. You create a widget, then you create a second widget, then you delete the first widget, and then you create a third widget.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 15:13

























answered Nov 15 '18 at 14:23









Bryan OakleyBryan Oakley

217k22264425




217k22264425













  • That is the thing I was trying to show though. Even if I destroy before clearing it will still keep counting. I tried to show this with the list[0].destroy, which does the same as your for loop (as long as it has just 1 entry ofc). Doing it via a for loop does not change the outcome, just tried.

    – Rattja
    Nov 15 '18 at 14:44











  • "That is the thing I was trying to show though. Even if I destroy before clearing it will still keep counting. " - your code doesn't illustrate that, and what you say contradicts how tkinter works. The fact that it keeps counting is inconsequential -- the number can go to 10000 and still work.

    – Bryan Oakley
    Nov 15 '18 at 14:52











  • I see now, destroying before clearing does fix the problem. I thought it did not work as I could still see it keep the count. Tested it by generating one frame and clearing the list 50 000+ times with Tkinter window open. When it's done, closing the window with the x takes a long time. If I add the destroy() before clearing it's instant. So I guess that if you don't destroy, and keep generating a huge amount of frames over and over, you'll run into memory problems.

    – Rattja
    Nov 15 '18 at 15:52



















  • That is the thing I was trying to show though. Even if I destroy before clearing it will still keep counting. I tried to show this with the list[0].destroy, which does the same as your for loop (as long as it has just 1 entry ofc). Doing it via a for loop does not change the outcome, just tried.

    – Rattja
    Nov 15 '18 at 14:44











  • "That is the thing I was trying to show though. Even if I destroy before clearing it will still keep counting. " - your code doesn't illustrate that, and what you say contradicts how tkinter works. The fact that it keeps counting is inconsequential -- the number can go to 10000 and still work.

    – Bryan Oakley
    Nov 15 '18 at 14:52











  • I see now, destroying before clearing does fix the problem. I thought it did not work as I could still see it keep the count. Tested it by generating one frame and clearing the list 50 000+ times with Tkinter window open. When it's done, closing the window with the x takes a long time. If I add the destroy() before clearing it's instant. So I guess that if you don't destroy, and keep generating a huge amount of frames over and over, you'll run into memory problems.

    – Rattja
    Nov 15 '18 at 15:52

















That is the thing I was trying to show though. Even if I destroy before clearing it will still keep counting. I tried to show this with the list[0].destroy, which does the same as your for loop (as long as it has just 1 entry ofc). Doing it via a for loop does not change the outcome, just tried.

– Rattja
Nov 15 '18 at 14:44





That is the thing I was trying to show though. Even if I destroy before clearing it will still keep counting. I tried to show this with the list[0].destroy, which does the same as your for loop (as long as it has just 1 entry ofc). Doing it via a for loop does not change the outcome, just tried.

– Rattja
Nov 15 '18 at 14:44













"That is the thing I was trying to show though. Even if I destroy before clearing it will still keep counting. " - your code doesn't illustrate that, and what you say contradicts how tkinter works. The fact that it keeps counting is inconsequential -- the number can go to 10000 and still work.

– Bryan Oakley
Nov 15 '18 at 14:52





"That is the thing I was trying to show though. Even if I destroy before clearing it will still keep counting. " - your code doesn't illustrate that, and what you say contradicts how tkinter works. The fact that it keeps counting is inconsequential -- the number can go to 10000 and still work.

– Bryan Oakley
Nov 15 '18 at 14:52













I see now, destroying before clearing does fix the problem. I thought it did not work as I could still see it keep the count. Tested it by generating one frame and clearing the list 50 000+ times with Tkinter window open. When it's done, closing the window with the x takes a long time. If I add the destroy() before clearing it's instant. So I guess that if you don't destroy, and keep generating a huge amount of frames over and over, you'll run into memory problems.

– Rattja
Nov 15 '18 at 15:52





I see now, destroying before clearing does fix the problem. I thought it did not work as I could still see it keep the count. Tested it by generating one frame and clearing the list 50 000+ times with Tkinter window open. When it's done, closing the window with the x takes a long time. If I add the destroy() before clearing it's instant. So I guess that if you don't destroy, and keep generating a huge amount of frames over and over, you'll run into memory problems.

– Rattja
Nov 15 '18 at 15:52




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300973%2fresetting-generated-widget-count-in-python-with-tkinter%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

The Sandy Post

Danny Elfman

Pages that link to "Head v. Amoskeag Manufacturing Co."