I have the following code which should display text in a scrollable window. I need two buttons on the scrollable window and hence I am using askokcancel().
from Tkinter import *
import tkMessageBox
import Tix
from bs4 import BeautifulSoup
import urllib
class Window:
def __init__(self, root,msg):
self.myMainContainer = Frame(width=500,height=500)
self.myMainContainer.pack()
#self.myMainContainer.grid(row=0, column=0)
scrwin = Tix.ScrolledWindow(self.myMainContainer, scrollbar='y')
scrwin.pack()
#scrwin.grid(row=0, column=0)
self.win = scrwin.window
w = tkMessageBox.askokcancel('this', msg)
print w
link = 'http://www.mnemonicdictionary.com/word/'
word = 'scrap'
r = urllib.urlopen(link+word)
soup = BeautifulSoup(r,'html.parser')
result = soup.find_all('div',id='home-middle-content')
text = str(result[0].get_text().encode('utf-8')).replace('\n','')
print text
root = Tix.Tk()
myWindow = Window(root,text)
root.mainloop()
However I get two windows : One containing the text and the other containing the scroll bar. How do I combine them ?
Kindly Help...
No sure, which version of Python
and Tkinter
, you are using. But If I have to do something like this now, with python3
, I will do it like below.
from tkinter import *
from tkinter.scrolledtext import ScrolledText
from tkinter.constants import END
class SampleDialog(object):
def __init__(self, parent):
self.root=Toplevel(parent)
self.text = ScrolledText(self.root)
self.text.insert(END, "a\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\n")
self.text.pack()
self.ok = Button(self.root, text="ok", command=self.onOk)
self.ok.pack()
self.root.wait_visibility()
self.root.grab_set()
self.root.transient(parent)
self.parent = parent
def onOk(self):
self.root.grab_release()
self.root.destroy()
class MainWindow:
def __init__(self, window):
Button(window, text='dialog', command=self.onDialog).pack()
Button(window, text='exit', command=self.onExit).pack()
self.window = window
def onDialog(self):
d = SampleDialog(self.window)
self.window.wait_window(d.root) # <<< NOTE
def onExit(self):
self.window.destroy()
root = Tk()
app = MainWindow(root)
root.mainloop()