Can anyone please explain why there are some functions unresolved like insert() and pack() for text widget particularly and there is an error in text = Text(root) line?
I have imported Tkinter and set PYTHONPATH to libs but I still can not run programm normally.
Thank you in advance
from Tkinter import *
import tkFileDialog
from nltk import *
import sentiment_analysis
root = Tk()
root.title('Semantic Orientation of the Text')
frame = Frame(root)
frame.pack()
text = Text(root)// error
text.tag_config("big", font=('Verdana', 14, 'normal'))
text.tag_config("color", font=('Times New Roman', 24))
text.tag_config("groove", relief=GROOVE, borderwidth=4)
text.pack(expand=YES, fill=BOTH) #pack() is unresolved
scroll = Tk.Scrollbar(text)
scroll.pack(side=RIGHT, fill=Y)
def onButtonText():
filename = tkFileDialog.askopenfilename(initialdir='C:/nltk_data/sentiment_analysis')
text.insert(END, open(filename).read()) #insert() in unresolved
There are also other functions for buttons' event handlers but they have the same mistake - insert() for text widget is unresolved
My guess is, since you are doing import *, you're importing two versions of Text so you're not getting the object you think you are.
There's really no good reason to do import *. Your code will be easier to maintain if you do something like this instead:
import Tkinter as tk
...
root = tk.Tk()
text = tk.Text(root, ...)
Related
I am trying to learn how to use Tkinter, but whenever I want to execute my code I always get this problem: (NameError: name 'label' is not defined) or (NameError: name 'button' is not defined).
As a beginner, it seems to me that the problem is with my code editor. (BTW I am using VScode)
this is my code:
from tkinter import *
root = Tk()
mylabel = label(root, text='Hello World')
mylabel.pack()
root.mainloop()
And as I said, this also happens with this one:
from tkinter import *
root = Tk()
mybutton = button(root, text='Hello World')
mybutton.pack()
root.mainloop()
you have caps error its Button and Label not button and label
I'm working on an Email Spam Classifier model and when I wanted to make a GUI, tkinter package is showing errors like
TclError: unknown option "-font"
TclError: unknown option "-borderwidth"
TclError: unknown option "-bg"
TclError: unknown option "-relief"
It seems like none of the functionalities of the Tkinter package seems to work. I've tried a lot of methods but still no result.
Please help me.
I've attached my code below
import tkinter as tk
from tkinter import *
from tkinter.ttk import *
from ttkthemes import ThemedTk
def GUI():
root = tk.Tk()
root.geometry('500x200')
root.maxsize(500,200)
root.minsize(500,200)
root.title('Email Spam Classifier')
root['bg'] = "white"
title = Label(root,text="Email Spam Classifier",font=('verdana',15,'bold'))
title.place(x=180,y=5)
Label(root,text="Enter mail to Classify ",font=('verdana',10,'bold')).place(x=50,y=50)
url = Entry(root,width=50)
url.place(x=50,y=80)
button = Button(root,text="Predict",font=('verdana',8,'bold'))
button.place(x=360,y=78)
root.mainloop()
GUI()
Please help me with this.
I'm new to this community, so if I had done some mistake in alignment of the text or method of asking, excuse me please.
Thanks in advance!
The problem is wildcard import.
The Button class is present in both the tkinter module and the tkinter.ttk module.
If the tkinter.ttk import follows the tkinter import, it overrides widgets with the same class names. And vice versa.
In your code, the button is tkinter.ttk.Button.
The style properties for this button are set differently.
If you want to use tkinter.Button, you need to explicitly specify which button it is.
button = tk.Button(root,text="Predict",font=('verdana',8,'bold'))
If you are using ttkthemes, you need to use ttk widgets to apply the theme to them.
import tkinter.ttk as ttk
from ttkthemes import ThemedTk
def GUI():
root = ThemedTk(theme="arc") # light theme
root.geometry('630x200')
root.maxsize(630, 200)
root.minsize(630, 200)
root.title('Email Spam Classifier')
# this will work too:
# root["background"] = "white"
# root.config(background="white")
style = ttk.Style()
# font for all ttk Buttons
style.configure("TButton", font=('verdana',8,'bold'))
# font for all ttk Labels
style.configure("TLabel", font=('verdana',10,'bold'))
# font for a specific ttk Label
style.configure("Title.TLabel", font=('verdana',15,'bold'))
title = ttk.Label(root, text="Email Spam Classifier",
style="Title.TLabel")
title.place(x=180, y=5)
enter_label = ttk.Label(root, text="Enter mail to Classify ")
enter_label.place(x=50, y=50)
url = ttk.Entry(root, width=50)
url.place(x=50, y=80)
print(url.winfo_reqwidth()) # 412
# the button has been moved because it is overlapping the entry
button = ttk.Button(root, text="Predict")
button.place(x=412+50, y=79)
print(button.winfo_reqwidth())
root.mainloop()
GUI()
I have recently being trying to make pokedex using tkinter python and I was going well with it but a problem occurred, which made the whole interface ugly!
There is a dotted border which is coming in every active button and notebook tab which is spoiling look of the whole program.
Also the code of styling didn't worked out !
NOTE: I tried using focuscolor but it didn't help...
import tkinter as tk
from tkinter.ttk import *
root = tk.Tk()
root.geometry('700x500')
Style().configure('lefttab.TNotebook', tabposition='wn')
Style().configure('TNotebook.Tab', font='Courier 16')
notebook = Notebook(root, style='lefttab.TNotebook')
f1 = tk.Frame(notebook, width=500, height=400)
f2 = tk.Frame(notebook, width=500, height=400)
notebook.add(f1, text='Kantodex Vx')
notebook.add(f2, text=' Attacks ')
notebook.pack(anchor=tk.NW)
root.mainloop()
I want to link GUI (1. modul) with the functions that are in a different module. Basically I need to link GUI with the program.
I created a very easy example:
modul1:
from modul2 import *
from tkinter import *
window = Tk()
window.title('Program')
window.geometry("300x300")
text_input= StringVar()
#result
result=Entry(window, textvariable=text_input)
result.place(x=6,y=15)
#Button
button=Button(window, text='X')
button.config(width=5, height=2, command=lambda: test())
button.place(x=10,y=70)
window.mainloop()
modul2:
import modul1
def test():
global text_input
text_input.set('hello')
EXPECTED:
This program should write "hello" into Entry window after clicking on a button.
RESULT: ERROR:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Ondrej\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:/Users/Ondrej/Desktop/VUT FIT/IVS\modul1.py", line 17, in <lambda>
button.config(width=5, height=2, command=lambda: test())
NameError: name 'test' is not defined
Does anybody know where the problem is?
Thank you in advance for the help.
Yes, the problem you actually have is to do with a circular import. Modul1 imports Modul2 and Modul2 imports Modul1. a way you could solve this is to give the textinput as a parameter to the test function in modul2 like this:
modul1.py:
import modul2
from tkinter import *
window = Tk()
window.title('Program')
window.geometry("300x300")
text_input = StringVar()
# result
result = Entry(window, textvariable=text_input)
result.place(x=6, y=15)
# Button
button = Button(window, text='X')
button.config(width=5, height=2, command=lambda: modul2.test(text_input))
button.place(x=10, y=70)
window.mainloop()
modul2.py:
def test(text_input):
text_input.set('hello')
Unfortunately python really doesn't like you to do circular imports and this is good as this would actually mean an infinite loop. When would it stop importing the other file?
Edit: there would be a very convoluted way to make a class in a third module and set attributes on that, and then import this third module in both modul1 and modul2 to share variables between them but please don't...
Edit2: an example of this:
Modul1.py:
from shared import SharedClass
import modul2
from tkinter import *
window = Tk()
window.title('Program')
window.geometry("300x300")
SharedClass.text_input = StringVar()
# result
result = Entry(window, textvariable=SharedClass.text_input)
result.place(x=6, y=15)
# Button
button = Button(window, text='X')
button.config(width=5, height=2, command=lambda: modul2.test())
button.place(x=10, y=70)
window.mainloop()
Modul2.py:
from shared import SharedClass
def test():
SharedClass.text_input.set('hello')
shared.py
class SharedClass:
pass
This works due to the way python loads imported classes. They all are the same. This makes it so if you set properties on the class (not the instances of it) you can share the properties.
I am starting to learn Python and the tkinter package and I am writing a program to load a text file on the GUI window. To open the file browser, I installed the button and its necessary function as shown in the below code. The program runs but when I click on the "browse" button, I am getting an attribute error saying : "'assign_1' object has no attribute 'var_filename'". It would be great if anyone could help me with this.
from tkinter import *
from tkinter import messagebox
from tkinter import simpledialog
from tkinter import filedialog
from math import *
from numpy import *
import string
root = Tk()
def close_window_callback(root):
if messagebox.askokcancel("Quit", "Do you really wish to quit?"):
root.destroy()
class assign_1:
def __init__(self,master):
self.master = master
frame = Frame(master)
frame.pack()
self.canvas = Canvas(master,width=1000,height=1000, bg="yellow")
self.button_browse = Button(frame, text="Browse",
command=self.browse_file)
self.button_browse.pack()
self.button_load = Button(frame, text="Load")
self.button_load.pack(side = LEFT)
self.canvas.pack(expand=YES, fill=BOTH)
def browse_file(self):
self.var_filename.set(filedialog.askopenfilename(filetypes=[("allfiles","*"),("pythonfiles","*.txt")]))
filename = self.var_filename.get()
print(filename)
root.protocol("WM_DELETE_WINDOW", lambda root_window=root: close_window_callback(root_window))
assign_1(root)
root.mainloop()
Although, as Rinzler pointed out, your indentation is wrong in the code you posted, that would lead to another error (AttributeError: assign_1 instance has no attribute 'browse_file'). So I'm guessing the indentation in the code you actually use is correct.
The problem is that you try to use self.var_filename.set(...) without having defined what self.var_filename is. If you want it to be a StringVar, which seems to be the case since you use set and get, you have to initialize it. To do this you should put self.var_filename = StringVar(master) in the class' __init__ function. A small example demonstrating this:
root = Tk()
class assign_1:
def __init__(self, master):
self.master = master
self.var_filename = StringVar(master)
self.button_browse = Button(master, text="Browse", command=self.browse_file)
self.button_browse.pack()
def browse_file(self):
self.var_filename.set(filedialog.askopenfilename(filetypes=[("allfiles","*"),("pythonfiles","*.txt")]))
filename = self.var_filename.get()
print(filename)
assign_1(root)
root.mainloop()
However, from the looks of it, in your case there is no need to use a tkinter StringVar, just use a normal string variable:
root = Tk()
class assign_1:
def __init__(self, master):
self.master = master
self.button_browse = Button(master, text="Browse", command=self.browse_file)
self.button_browse.pack()
def browse_file(self):
self.filename = filedialog.askopenfilename(filetypes=[("allfiles","*"),("pythonfiles","*.txt")])
print(self.filename)
assign_1(root)
root.mainloop()
The indentation is wrong. The function browse_file you wanted to define as method of the class assign_1 (use capitalise letters to declare name of classes) is a global function as you defined it.
You have also not defined self.var_filename anywhere, so it will then give you the error:
AttributeError: 'assign_1' object has no attribute 'var_filename'
Under the function close_window_callback, you have also wrong indentation.