python - When importing modules written with tkinter and ttk, stuff doesn't work -


i'm new programming, python, website, , using these kinds of websites in general, hear me out.

i've been writing module larger program using tkinter module , ttk module, , when import own module main program, reason none of ttk stuff works should. mean, appears, style i've written (s=ttk.style(); s.configure...etc.) doesn't change in anyway. when run module on own, works fine. when it's imported main program, doesn't.

not this, when using entry boxes, i've discovered way i'd been told use them, with, example, var=stringvar() textvariable (which again works fine when module run on own), leaves variable var empty when var.get() called. i've sorted removing mention of stringvar() (wish i'd known how redundant these are), i'd still know why importing them in main program causes them malfunction badly. give sample code there's i'd struggle selective enough...

i'd appreciate guidance can offer.

edit: giving have helped?

stackoverflowmodule.py

import sys tkinter import * tkinter import ttk import time random import randint, choice  class decimals():     def question1(self):         decframe.destroy()         frame1=ttk.frame(decmaster, height=height, width=width, style="newframe.tframe")         frame1.pack()         q1label=ttk.label(frame1, text="question 1:", style="titlelabel.tlabel")         q1label.grid(column=0, row=0, pady=(50,0))              answer=stringvar()         entry1=ttk.entry(frame1, textvariable=answer)         entry1.grid(column=0, row=1, pady=(200,0))         # typing in hello should give correct answer.         def question1attempt():                         attempt=answer.get()                         if attempt!="hello":                 print("incorrect")             else:                 print("correct")         button=ttk.button(frame1, text="ok", command=question1attempt)         button.grid(column=0, row=2, pady=(30,0))   def start():     global decmaster     global s     global decframe     global dec     global width     global height     decmaster = tk()      width=str(1000)     height=str(800)     x1=str(0)     y1=str(0)      decmaster.geometry(width+"x"+height+"+"+x1+"+"+y1)     decmaster.configure(bg="#8afff0")      s=ttk.style()     s.configure("newframe.tframe", background="#8afff0")     s.configure("titlelabel.tlabel", foreground= "blue", background="#8afff0")      decframe=ttk.frame(decmaster, style="newframe.tframe")     decframe.pack()      titlelabel=ttk.label(decframe, text="test decimals", style="titlelabel.tlabel")     titlelabel.grid(column=1, row=0, pady=(50,0), sticky=n)      dec=decimals()     button=ttk.button(decframe, text="start", command=dec.question1)     button.grid(column=2, row=2, pady=(200,0), sticky=n)      decmaster.mainloop() 

stackoverflowprogram.py

from tkinter import * tkinter import ttk import time import stackoverflowmodule    root = tk()  width=str(1000) height=str(800) x1=str(0) y1=str(0) ##width=str(1228) ##height=str(690) ##x1=str(-1) ##y1=str(-22)  root.geometry(width+"x"+height+"+"+x1+"+"+y1) root.configure(bg="#8afff0")  s=ttk.style() s.configure("newframe.tframe", background="#8afff0") s.configure("titlelabel.tlabel", foreground= "blue", background="#8afff0")  testframe=ttk.frame(root, height=height, width=width, style="newframe.tframe") testframe.pack() titlelabel=ttk.label(testframe, text="start test:", style="titlelabel.tlabel") titlelabel.grid(column=0, row=0, pady=(50,0))  def starttest():     stackoverflowmodule.start()   button=ttk.button(testframe, text="start", command=starttest) button.grid(column=0, row=1, pady=(100,0))  root.mainloop() 

i realise there's awful lot there, couldn't demonstrate point without all. again.

the root of problem you're creating more once instance of tk. tkinter app can have single instance of of tk class, , must call mainloop once. if need additional windows should create instances of toplevel (http://effbot.org/tkinterbook/toplevel.htm).

if want create modules reusable code, have modules create subclasses of frame (or toplevel if you're creating dialos). then, main script create instance of tk, , place these frames in main window or in subwindows.

if want use module reusable component , runnable program, put "runnable program" part inside special if statement:

# module1.py import tkinter tk class module1(tk.frame):     def __init__(self, *args, **kwargs):         label = tk.label(self, text="i module 1")         label.pack(side="top", fill="both", expand=true)  # code not run if module imported if __name__ == "__main__":     root = tk.tk()     m1 = module1(root)     m1.pack(side="top", fill="both", expand=true) 

in above code, if run python module1.py, code in final if statement run. create root window, create instance of frame, , make frame fill main window.

if, however, import above code program, code in if statement not run, don't more 1 instance of tk.

let's assume have 2 modules above, , want write program uses them, , each should go in separate window. can writing third script uses them both:

# main.py import tkinter tk module1 import module1 module2 import module2  # create main window; every tkinter app needs # 1 instance of class root = tk.tk() m1 = module1(root) m1.pack(side="top", fill="both", expand=true)  # create second window second = tk.toplevel(root) m2 = module2(second) m2.pack(side="top", fill="both", expand=true)  # run event loop root.mainloop() 

with above, have code in 2 modules can used in 3 ways: standalone programs, separate frames within single window, or separate frames within separate windows.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -