OOP with tkinter GUI: Struggling with variable scope and string slicing





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm just starting to learn Python and am having trouble getting my GUI to work with my application. Here's what I want it it do:



1) User inputs String < 100 characters into textbox



2) Program slices 5 characters at a time from String and counts how many of letter "A" and "B" there are in substring



3) Program prints 5-character substring and # of each letter to GUI textbox



Here's what I have so far:



from tkinter import *
from tkinter.ttk import *

class App(Frame):

def __init__(self):
super().__init__()

# Global variables
self.input_text = StringVar
self.output_text = StringVar
self.letter_A = 0
self.letter_B = 0
self.launchGUI()

def launchGUI(self):
self.button_selected_string = StringVar()
self.pack(fill=BOTH, expand=True)

self.topFrame = Frame(self)
self.topFrame.grid(row=0)
self.bottomFrame = Frame(self)
self.bottomFrame.grid(row=2)

# Input textbox
self.input_textbox = Text(self.topFrame, height=5, width=50, borderwidth=2, relief=RIDGE)
self.input_textbox.grid(row=1)

# Output textbox
self.output_textbox = Text(self.bottomFrame, height=5, width=50, borderwidth=2, relief=RIDGE)
self.output_textbox.grid(row=2)

# Submit button
self.submitButton = Button(self.bottomFrame, text="Submit", width=30, command=self.print_result)
self.submitButton.grid(row=1)

# Radio button
self.radioButton1 = Radiobutton(self.bottomFrame, text="Name of Button", variable=self.button_selected_string,
value="BUTTON_SELECTED",
command=self.get_button_state)
self.radioButton1.grid(row=0)

# Function for printing result in textbox
def print_result(self):
self.iterate() # Calls another method
self.output_textbox.insert("1.0", str(self.output_text) + str(self.letter_A) + str(self.letter_B))

# Function that retrieves result of user selection and stores as String
def get_button_state(self):
self.button_selected_string.get()

def iterate(self):
temp = self.input_textbox.get("1.0", "end-1c")
if self.get_button_state() == "BUTTON_SELECTED": # Will always be selected for the purpose of example
i = 100
while i > 20:
temp = temp[(i - 5):i]
self.output_text = self.count_letters(temp)
i = i - 1
return self.output_text

def count_letters(self):

for y in self.output_text:
for x in y:
if x == 'A' or x == 'a':
self.letter_A += 1
elif x == 'B' or x == 'b':
self.letter_B += 1
return self.output_text

def main():
root = Tk()
root.geometry("350x300+300+300")
app = App()
root.mainloop()


if __name__ == '__main__':
main()


The problem (among many, I'm sure) is that when I try and print the results, instead of getting a list of 5-character long substrings and the number of letters "A" and "B", I just get:



<class 'tkinter.StringVar'>00


It seems like I have a type error and am probably messing up with the scope of my variables, but I'm not sure how to proceed...



Any help would be GREATLY appreciated!










share|improve this question

























  • There are couple problems here, it's to broad for SO. Read about The Variable Classes (BooleanVar, DoubleVar, IntVar, StringVar) and Python - Functions and how to use The pdb Module

    – stovfl
    Nov 17 '18 at 9:52


















0















I'm just starting to learn Python and am having trouble getting my GUI to work with my application. Here's what I want it it do:



1) User inputs String < 100 characters into textbox



2) Program slices 5 characters at a time from String and counts how many of letter "A" and "B" there are in substring



3) Program prints 5-character substring and # of each letter to GUI textbox



Here's what I have so far:



from tkinter import *
from tkinter.ttk import *

class App(Frame):

def __init__(self):
super().__init__()

# Global variables
self.input_text = StringVar
self.output_text = StringVar
self.letter_A = 0
self.letter_B = 0
self.launchGUI()

def launchGUI(self):
self.button_selected_string = StringVar()
self.pack(fill=BOTH, expand=True)

self.topFrame = Frame(self)
self.topFrame.grid(row=0)
self.bottomFrame = Frame(self)
self.bottomFrame.grid(row=2)

# Input textbox
self.input_textbox = Text(self.topFrame, height=5, width=50, borderwidth=2, relief=RIDGE)
self.input_textbox.grid(row=1)

# Output textbox
self.output_textbox = Text(self.bottomFrame, height=5, width=50, borderwidth=2, relief=RIDGE)
self.output_textbox.grid(row=2)

# Submit button
self.submitButton = Button(self.bottomFrame, text="Submit", width=30, command=self.print_result)
self.submitButton.grid(row=1)

# Radio button
self.radioButton1 = Radiobutton(self.bottomFrame, text="Name of Button", variable=self.button_selected_string,
value="BUTTON_SELECTED",
command=self.get_button_state)
self.radioButton1.grid(row=0)

# Function for printing result in textbox
def print_result(self):
self.iterate() # Calls another method
self.output_textbox.insert("1.0", str(self.output_text) + str(self.letter_A) + str(self.letter_B))

# Function that retrieves result of user selection and stores as String
def get_button_state(self):
self.button_selected_string.get()

def iterate(self):
temp = self.input_textbox.get("1.0", "end-1c")
if self.get_button_state() == "BUTTON_SELECTED": # Will always be selected for the purpose of example
i = 100
while i > 20:
temp = temp[(i - 5):i]
self.output_text = self.count_letters(temp)
i = i - 1
return self.output_text

def count_letters(self):

for y in self.output_text:
for x in y:
if x == 'A' or x == 'a':
self.letter_A += 1
elif x == 'B' or x == 'b':
self.letter_B += 1
return self.output_text

def main():
root = Tk()
root.geometry("350x300+300+300")
app = App()
root.mainloop()


if __name__ == '__main__':
main()


The problem (among many, I'm sure) is that when I try and print the results, instead of getting a list of 5-character long substrings and the number of letters "A" and "B", I just get:



<class 'tkinter.StringVar'>00


It seems like I have a type error and am probably messing up with the scope of my variables, but I'm not sure how to proceed...



Any help would be GREATLY appreciated!










share|improve this question

























  • There are couple problems here, it's to broad for SO. Read about The Variable Classes (BooleanVar, DoubleVar, IntVar, StringVar) and Python - Functions and how to use The pdb Module

    – stovfl
    Nov 17 '18 at 9:52














0












0








0








I'm just starting to learn Python and am having trouble getting my GUI to work with my application. Here's what I want it it do:



1) User inputs String < 100 characters into textbox



2) Program slices 5 characters at a time from String and counts how many of letter "A" and "B" there are in substring



3) Program prints 5-character substring and # of each letter to GUI textbox



Here's what I have so far:



from tkinter import *
from tkinter.ttk import *

class App(Frame):

def __init__(self):
super().__init__()

# Global variables
self.input_text = StringVar
self.output_text = StringVar
self.letter_A = 0
self.letter_B = 0
self.launchGUI()

def launchGUI(self):
self.button_selected_string = StringVar()
self.pack(fill=BOTH, expand=True)

self.topFrame = Frame(self)
self.topFrame.grid(row=0)
self.bottomFrame = Frame(self)
self.bottomFrame.grid(row=2)

# Input textbox
self.input_textbox = Text(self.topFrame, height=5, width=50, borderwidth=2, relief=RIDGE)
self.input_textbox.grid(row=1)

# Output textbox
self.output_textbox = Text(self.bottomFrame, height=5, width=50, borderwidth=2, relief=RIDGE)
self.output_textbox.grid(row=2)

# Submit button
self.submitButton = Button(self.bottomFrame, text="Submit", width=30, command=self.print_result)
self.submitButton.grid(row=1)

# Radio button
self.radioButton1 = Radiobutton(self.bottomFrame, text="Name of Button", variable=self.button_selected_string,
value="BUTTON_SELECTED",
command=self.get_button_state)
self.radioButton1.grid(row=0)

# Function for printing result in textbox
def print_result(self):
self.iterate() # Calls another method
self.output_textbox.insert("1.0", str(self.output_text) + str(self.letter_A) + str(self.letter_B))

# Function that retrieves result of user selection and stores as String
def get_button_state(self):
self.button_selected_string.get()

def iterate(self):
temp = self.input_textbox.get("1.0", "end-1c")
if self.get_button_state() == "BUTTON_SELECTED": # Will always be selected for the purpose of example
i = 100
while i > 20:
temp = temp[(i - 5):i]
self.output_text = self.count_letters(temp)
i = i - 1
return self.output_text

def count_letters(self):

for y in self.output_text:
for x in y:
if x == 'A' or x == 'a':
self.letter_A += 1
elif x == 'B' or x == 'b':
self.letter_B += 1
return self.output_text

def main():
root = Tk()
root.geometry("350x300+300+300")
app = App()
root.mainloop()


if __name__ == '__main__':
main()


The problem (among many, I'm sure) is that when I try and print the results, instead of getting a list of 5-character long substrings and the number of letters "A" and "B", I just get:



<class 'tkinter.StringVar'>00


It seems like I have a type error and am probably messing up with the scope of my variables, but I'm not sure how to proceed...



Any help would be GREATLY appreciated!










share|improve this question
















I'm just starting to learn Python and am having trouble getting my GUI to work with my application. Here's what I want it it do:



1) User inputs String < 100 characters into textbox



2) Program slices 5 characters at a time from String and counts how many of letter "A" and "B" there are in substring



3) Program prints 5-character substring and # of each letter to GUI textbox



Here's what I have so far:



from tkinter import *
from tkinter.ttk import *

class App(Frame):

def __init__(self):
super().__init__()

# Global variables
self.input_text = StringVar
self.output_text = StringVar
self.letter_A = 0
self.letter_B = 0
self.launchGUI()

def launchGUI(self):
self.button_selected_string = StringVar()
self.pack(fill=BOTH, expand=True)

self.topFrame = Frame(self)
self.topFrame.grid(row=0)
self.bottomFrame = Frame(self)
self.bottomFrame.grid(row=2)

# Input textbox
self.input_textbox = Text(self.topFrame, height=5, width=50, borderwidth=2, relief=RIDGE)
self.input_textbox.grid(row=1)

# Output textbox
self.output_textbox = Text(self.bottomFrame, height=5, width=50, borderwidth=2, relief=RIDGE)
self.output_textbox.grid(row=2)

# Submit button
self.submitButton = Button(self.bottomFrame, text="Submit", width=30, command=self.print_result)
self.submitButton.grid(row=1)

# Radio button
self.radioButton1 = Radiobutton(self.bottomFrame, text="Name of Button", variable=self.button_selected_string,
value="BUTTON_SELECTED",
command=self.get_button_state)
self.radioButton1.grid(row=0)

# Function for printing result in textbox
def print_result(self):
self.iterate() # Calls another method
self.output_textbox.insert("1.0", str(self.output_text) + str(self.letter_A) + str(self.letter_B))

# Function that retrieves result of user selection and stores as String
def get_button_state(self):
self.button_selected_string.get()

def iterate(self):
temp = self.input_textbox.get("1.0", "end-1c")
if self.get_button_state() == "BUTTON_SELECTED": # Will always be selected for the purpose of example
i = 100
while i > 20:
temp = temp[(i - 5):i]
self.output_text = self.count_letters(temp)
i = i - 1
return self.output_text

def count_letters(self):

for y in self.output_text:
for x in y:
if x == 'A' or x == 'a':
self.letter_A += 1
elif x == 'B' or x == 'b':
self.letter_B += 1
return self.output_text

def main():
root = Tk()
root.geometry("350x300+300+300")
app = App()
root.mainloop()


if __name__ == '__main__':
main()


The problem (among many, I'm sure) is that when I try and print the results, instead of getting a list of 5-character long substrings and the number of letters "A" and "B", I just get:



<class 'tkinter.StringVar'>00


It seems like I have a type error and am probably messing up with the scope of my variables, but I'm not sure how to proceed...



Any help would be GREATLY appreciated!







python oop tkinter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '18 at 7:19







Sam

















asked Nov 17 '18 at 7:11









SamSam

143




143













  • There are couple problems here, it's to broad for SO. Read about The Variable Classes (BooleanVar, DoubleVar, IntVar, StringVar) and Python - Functions and how to use The pdb Module

    – stovfl
    Nov 17 '18 at 9:52



















  • There are couple problems here, it's to broad for SO. Read about The Variable Classes (BooleanVar, DoubleVar, IntVar, StringVar) and Python - Functions and how to use The pdb Module

    – stovfl
    Nov 17 '18 at 9:52

















There are couple problems here, it's to broad for SO. Read about The Variable Classes (BooleanVar, DoubleVar, IntVar, StringVar) and Python - Functions and how to use The pdb Module

– stovfl
Nov 17 '18 at 9:52





There are couple problems here, it's to broad for SO. Read about The Variable Classes (BooleanVar, DoubleVar, IntVar, StringVar) and Python - Functions and how to use The pdb Module

– stovfl
Nov 17 '18 at 9:52












0






active

oldest

votes












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%2f53349065%2foop-with-tkinter-gui-struggling-with-variable-scope-and-string-slicing%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















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%2f53349065%2foop-with-tkinter-gui-struggling-with-variable-scope-and-string-slicing%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

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python