Admittedly I am a noob with Python but I know enough to know that something stupid is going on here and I cannot find any help on google. All I want to do is use Tkinter for a simple GUI but it's pretty hard to do that when I get an Import error any time I try to import and use a function from one.
I followed a tutorial for this one with my own naming etc:
#testUI.py
from tkinter import *
from test2 import nameit
root = Tk()
root.title('Scott Window')
root.geometry('500x350')
greet = nameit('John')
mylabel = Label(root, text=greet)
mylabel.pack(pady=20)
root.mainloop()
#test2.py
def nameit(name):
greeting = name
return greeting
Using this yields:
ImportError: cannot import name 'nameit' from 'test2'
The other way that I have tried is using just "import"
from tkinter import *
import test2
root = Tk()
root.title('Scott Window')
root.geometry('500x350')
greet = test2.nameit('John')
mylabel = Label(root, text=greet)
mylabel.pack(pady=20)
root.mainloop()
This yields an error as well:
AttributeError: module 'test2' has no attribute 'nameit'
I am really at a loss as of what to do, again I'm almost positive it's something stupid but I cannot for the life of me find anything on google, on stackoverflow or anywhere else.
10000 Lifetimes of prosperity to anyone who can help me with this. Thank you!
The example you provided worked on my end, however, this problem may be because of some of the imports you are making in your test2.py file.
For example, in testUI.py you are importing test2 and in test2.py you are importing testUI. You need to find away to break this cycle and reduce the dependence.
An example is shown below:
# testUI.py
from tkinter import *
from test2 import nameit
root = Tk()
root.title('Scott Window')
root.geometry('500x350')
greet = nameit('John')
mylabel = Label(root, text=greet)
mylabel.pack(pady=20)
root.mainloop()
# test2.py
import testUI # Extra import statement
def nameit(name):
greeting = name
return greeting
When you run test2.py, you get the desired result.
When you run testUI.py, however, you get the following error:
from test2 import nameit
ImportError: cannot import name 'nameit' from partially initialized module 'test2' (most likely due to a circular import)
Related
I have created a simple tKinter Gui with PAGE builder and I am able to click a button and execute the corresponding command function within it. But when I try to get a value of a specific text box within the function I get various errors mostly no such property found. I have tried adding self and the class name into the property and even passing the property from the class as well as making it a function within that class but I still can't seem to access the values of the textbox 'Username'. I would really appreciate any help on how to get those text box values within the function as I have been researching for hours but still cannot make it work. Also if anyone knows of any good tutorial on this topic would help tremendously. Thank you.
The project has 2 files: (I've tried to remove the non essential code)
MacUpdaterPageDesign.py
import sys
import tkinter as tk
import tkinter.ttk as ttk
from tkinter.constants import *
import os.path
_script = sys.argv[0]
_location = os.path.dirname(_script)
import MacUpdaterPageDesign_support
class Toplevel1:
def __init__(self, top=None):
top.title("Mac Updater")
top.configure(background="#d9d9d9")
self.top = top
self.MainFrame = tk.Frame(self.top)
self.MainFrame.place(relx=0.0, rely=0.18, relheight=0.811
, relwidth=1.099)
self.Username = tk.Text(self.MainFrame)
self.Username.place(relx=0.15, rely=0.081, relheight=0.048
, relwidth=0.279)
#this button calls the CopyMACfunc on the support page
self.CopyMAC = tk.Button(self.MainFrame)
self.CopyMAC.place(relx=0.143, rely=0.846, height=34, width=117)
self.CopyMAC.configure(command=MacUpdaterPageDesign_support.CopyMACfunc)
self.CopyMAC.configure(text='Copy MAC')
MacUpdaterPageDesign_support.py
import sys
import tkinter as tk
import tkinter.ttk as ttk
from tkinter.constants import *
import MacUpdaterPageDesign
def main(*args):
'''Main entry point for the application.'''
global root
root = tk.Tk()
root.protocol( 'WM_DELETE_WINDOW' , root.destroy)
# Creates a toplevel widget.
global _top1, _w1
_top1 = root
_w1 = MacUpdaterPageDesign.Toplevel1(_top1)
root.mainloop()
def CopyMACfunc(*args):
#this part must retrieve the value in from Username
#tried many variations of below but throws error
username = MacUpdaterPageDesign.Username.get("1.0",END)
print(username)
if __name__ == '__main__':
MacUpdaterPageDesign.start_up()
Actually while writing this question I finally got it to work:
username = _w1.Username.get("1.0",END)
it did work with the following but not sure if this would be the right way to do it per say. Maybe their are better ways if anyone knows. Also would appreciate any recommendation for a good tutorial or where to learn all of this type of info. Thanks
When I'm trying to run a script to see if I can use tkinter on VsCode it throws a NameError saying name 'Tk' is not defined. Furthermore I can run it on IDLE and it runs just fine. I've been searching around to see if I can fix it but I still can't get it working. Do you have any idea what I'm doing wrong?
Here is the code:
from tkinter import *
root = Tk()
myLabel = Label(root, text = 'Hello World!')
myLabel.pack()
Do NOT name your file tkinter.py because the tkinter module you are trying to import is actually importing the file itself. And since there's no function called Tk in your file, you are getting that error. Rename the file to something else.
For example, rename it to gui.py.
Also, it's better to be explicit rather than implicit in python. So instead of
# Pollutes your namespace
# May clash with the functions you define or functions of other libraries that you import
from tkinter import *
root = Tk()
...
you should use
import tkinter as tk
root = tk.Tk()
...
Here's an example of how it can clash with other namespaces:
from tkinter import *
root = Tk()
Label = "hello"
Label1 = Label(gui, text=Label)
This results in an error:
Traceback (most recent call last):
File "stackoverflow.py", line 98, in <module>
Label1 = Label(gui, text=Label)
TypeError: 'str' object is not callable
I'm trying to include Autocomplete in my combobox but it's not working. Received an error when I'm trying to use tkentrycomplete. Below is the code I'm using for combobox, please point out my mistakes and help me out. Thank you
from tkinter import *
from tkinter.ttk import *
from tkinter import ttk
import tkinter as tk
import pandas as pd
comboExample1 = ttk.Combobox(window, width=30, values=list(df3["MFG Device"].unique()))
# comboExample1.current(0)
val = tk.StringVar()
comboExample1 = tkentrycomplete.AutocompleteCombobox(textvariable=val)
comboExample1.place(x=90, y=70)
comboExample1.bind("<<ComboboxSelected>>", select_device)
Error:
comboExample1 = tkentrycomplete.AutocompleteCombobox(textvariable=val)
NameError: name 'tkentrycomplete' is not define
tkentrycomplete is not a thing, I think what you need to do is comboExample1.AutocompleteCombobox(textvariable=val)
(I put this as an answer since I don't have enough rep to comment)
Edit:
try this:
comboExample1['values'] = val
(you may need to put this into a function and then bind it)
I'm trying to run this code which I got here:
from tkinter import *
import tkSimpleDialog
import tkMessageBox
root = Tk()
w = Label(root, text= "My Program")
w.pack()
tkMessageBox.showinfo("Welcome", "Add welcome message here")
but this
error occured:
I already look here ModuleNotFoundError: No module named x, but I can't understand the answers. I'm still new about these stuff.
If you are using python 3, you will need to import it as tkinter.simpledialog. Likewise, MessageBox has been changed to tkinter.messagebox.
I'm really stuck on a basilary things: I have this code
from tkinter import *
import sys
import subprocess
import tkinter as tk
def cd():
f=(subprocess.check_output("net view"))
e=(f.decode(sys.stdout.encoding))
label1=Label(text=e).pack()
def mainscreen():
mainscreen=Tk()
mainscreen.title("Terfysgol's kit V 2.0")
frame1=Frame(mainscreen)
frame1.pack()
puls1=Button(frame1,text="List of device", borderwidth= "2",command= cd).pack()
mainscreen()
When I run it all the time that I press the button it create a new label but I only want to update the text of the label1.
This is what you are after:
def cd():
f=(subprocess.check_output("net view"))
e=(f.decode(sys.stdout.encoding))
label1.config(text = e)
and then at the top of your program after your imports you need to put:
label1 = Label()
label1.pack()
Please note that I'm not suggesting this is good program structure, but that is up to you to sort out. This answer is just a quick fix to provide you with enough information to work out the rest of what you need.
Also you can remove the import tkinter as tk line already imports tkinter.