Tkinter: Merging 2 Windows
I've been playing around with my first Python Tkinter GUI. Below you van find te script I've made. Must be honest, I've looked around on the internet to find out how to do it. When
Solution 1:
The answer is easy: In the build_widgets method you are constructing a new Tk frame and tcl interpreter with
master = Tk()
You should never have two Tk() calls in your application. The solution is to delete this line and change every occurance of master to self. Self represent your app class, which inherits from the tk.Frame class and is therefore your main frame.
Also your construction of run_scipt is rather weird. Why don't you do it like this?
defrun_script(self):
inputs = self.read_tk_fields()
result = tennisMatchProbability.matchProb(inputs)
Here is the full code
from Tkinter import *
import sys
sys.path.append("C:\Users\Magali\Desktop\Tennis\tennisMatchProbability.py")
import tennisMatchProbability
classApp(Frame):
defrun_script(self):
inputs = self.read_tk_field()
result = tennisMatchProbability.matchProb(inputs)
self.show_prob_result(result)
defshow_prob_result(self,result):
self.result_label.config(text=result)
defbuild_widgets(self):
Label(self, text="First Name").grid(row=0)
Label(self, text="Last Name").grid(row=1)
Label(self, text="Game Score").grid(row=2)
Label(self, text="Set Score").grid(row=3)
e1 = Entry(self)
e2 = Entry(self)
e3 = Entry(self)
e4 = Entry(self)
self.result_label = Label(self)
e1.insert(10,"Novak")
e2.insert(10,"Djokovic")
e3.insert(10,"30-15")
e4.insert(10,"3-1")
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
e3.grid(row=2, column=1)
e4.grid(row=3, column=1)
self.result_label.grid(row=4, column=1)
Button(self, text='Run', command=self.run_script).grid(row=5, column=1, sticky=W, pady=4)
def__init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.build_widgets()
root = Tk()
app = App(master = root)
app.mainloop()
Solution 2:
@Jannick, with your script I don't get the results in the GUI. With your script I get the results in CMD and the "Run" bottun gives an error
With my code below I get the results in the GUI.
from Tkinter import *
import sys
sys.path.append("C:\Users\Magali\Desktop\Tennis\tennisMatchProbability.py")
classApp(Frame):
defrun_script(self):
sys.stdout = self
try:
del(sys.modules["tennisMatchProbability"])
except:
## Yeah, it's a real ugly solution...passimport tennisMatchProbability
tennisMatchProbability.matchProb()
sys.stdout = sys.__stdout__
defbuild_widgets(self):
self.text1 = Text(self)
self.text1.grid(row=5)
Label(self, text="First Name").grid(row=0)
Label(self, text="Last Name").grid(row=1)
Label(self, text="Game Score").grid(row=2)
Label(self, text="Set Score").grid(row=3)
e1 = Entry(self)
e2 = Entry(self)
e3 = Entry(self)
e4 = Entry(self)
e1.delete(0,END)
e2.delete(0,END)
e3.delete(0,END)
e4.delete(0,END)
e1.insert(10,"Novak")
e2.insert(10,"Djokovic")
e3.insert(10,"30-15")
e4.insert(10,"3-1")
e1.grid(row=0,column=1)
e2.grid(row=1, column=1)
e3.grid(row=2, column=1)
e4.grid(row=3, column=1)
Button(self,text='Run', command=self.run_script).grid(row=4, column=1, sticky=W, pady=4)
defwrite(self, txt):
self.text1.insert(INSERT, txt)
def__init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.build_widgets()
root = Tk()
app = App(master = root)
app.mainloop()
Post a Comment for "Tkinter: Merging 2 Windows"