This is my first post here, but I cannot find a solution elsewhere.
I'm using Python's Tkinter library to create a minimal Hashcat GUI Hash-Cracker, just for fun :)
It takes in a .hccapx hash file and a .dict dictionary file from sub-directories added to Hashcat-5.1.0 on Windows 10.
My issue is marked here:
def browsehccapx():
if path.exists(".\selected.hccapx"):
os.remove(".\selected.hccapx")
else:
pass
hccapxpath = filedialog.askopenfilename(initialdir=".\hccapxfiles", title="Select handshake .hccapx file",filetypes=((".hccapx files","*.hccapx"),("all files","*.*")))
hccapxpath = shutil.copy(hccapxpath, rootdir)
os.rename("{}".format(hccapxpath), ".\selected.hccapx")
and this is the Entry box that wont display the variable hccapxpath:
hccapxpathtxt = Entry(window, textvariable=hccapxpath, width=64, bg="white")
Can someone tell me why hccapxpathtxt will not display hccapxpath inside the Entry() box after browsehccapx() is called?
Do I need a different method to allow browsehccapx() to output the selected file path to the Entry() box?
Right now the program works perfectly fine when configured inside Hashcat directory, the only problem remaining is that the .hccapx file and .dict wordlist file are not displayed once selected.
All help is greatly appreciated! So is constructive criticism! Thank you in advance to everyone who takes time to try to help :)
The full python file can be found here: https://pastebin.com/6GQxSxfb
I fixed it. Thank you #acw1668. I defined hccapxpath as a StringVar() under global variables, then ran the following updated function to get the Entry() box to display the path:
def browsehccapx():
if path.exists(".\selected.hccapx"):
os.remove(".\selected.hccapx")
else:
pass
fullhccapxpath = filedialog.askopenfilename(initialdir=".\hccapxfiles", title="Select handshake .hccapx file",filetypes=((".hccapx files","*.hccapx"),("all files","*.*")))
fullhccapxpath = shutil.copy(fullhccapxpath, rootdir)
hccapxpath.set(fullhccapxpath)
os.rename("{}".format(fullhccapxpath), ".\selected.hccapx")
The pastebin link is good for 6 months.
I hope this helps others with a similar issue in the future!
Related
I am running tkinter library in the Odoo15 for a specific purpose.
I have created a custom python interpreter to run python code inside odoo.
To handle user inputs i specially designed a concept and via tkinter i am taking inputs from user.
In a code there may be more than one inputs and so i need to open the window more than one time. for example taken one input, Entered, closed window and then repeat the same process till final user input.
So in that case, at some movement my server getting terminated with a runtime error:
Tcl_AsyncDelete: cannot find async handler
Aborted (core dumped)
can anyone guide me how can i resolve this please ?
import tkinter as tk
import gc
root=tk.Tk()
root.geometry('800x200+600+300')
name_var=tk.StringVar()
var_1 = ''
def submit():
name=name_var.get()
global %s
var_1 = name
root.destroy()
name_label = tk.Label(root, text = 'Enter value', font=('calibre',10, 'bold'))
name_entry = tk.Entry(root,textvariable = name_var, font=('calibre',10,'normal'))
sub_btn=tk.Button(root,text = 'Submit', command = submit)
name_label.grid(row=0,column=0)
name_entry.grid(row=0,column=1)
sub_btn.grid(row=2,column=1)
root.mainloop()
when there is a line like value = input("Enter value")
i am replacing that line with the above code to take user input.
Looking forward to hear on this .. thanks
Without a minimal reproducible example it is more of a guessing than an answer.
But due to your request, I'll try my best guess. The information how you code this section below, can be crucial.
In a code there may be more than one inputs
and so i need to open the window more than
one time. for example taken one input,
Entered, closed window and then repeat the
same process till final user input.
If you destroy the root window, the instance of tkinter.Tk() and you try to retrieve the users input (data located in tkinter) of the destroyed instance you could run into trouble. Instead of creating new instances of tkinter.Tk use tkinter.Toplevel. You could hide the root window in many ways. I.e with overrideredirect and transparency.
TL;DR
Make sure you use the same instance of tkinter.Tk() through out the whole session and or connect the new instance with your server and vice versa.
Hope it will work out for you, especially because I don't know about odoo.
Please kick here to see the current output and expected output
I have a simple python program where i want to deselect the checkbutton by default. I want to see it the same way as when a user unchecks a tick box. Please let me know how to achieve it.
from tkinter import *
from tkinter import ttk
def urgentReq():
global box
state = box.state()
if(box.instate(['selected'])):
print ("--> Urgent: ",state)
else:
print ("--> Not Urgent:",state)
gui = Tk()
gui.title("GUI")
gui.geometry('200x150')
box = ttk.Checkbutton(gui, text ='Urgent Request', command=lambda: urgentReq())
box.grid(column=1, row=4, pady=40, sticky="N")
#write something here to unselect the box by default
box.state(['!alternate']) #box appear unchecked
box.state(['selected']) #box appear checked
Use the .invoke() method, but from what I read elsewhere is will also call the command, if one is associated. The instance I was trying to use this, my check button didn't have a command as a parameter, so this worked perfectly for me.
Hope this helps and good luck!
Somehow the initial state of the CheckButton = ('alternate',).
There is a workaround I found here: tkk checkbutton appears when loaded up with black box in it. If you apply it to your code like this, it seems to work:
checkVar = IntVar()
box = ttk.Checkbutton(gui, text ='Urgent Request', command=lambda: urgentReq(), variable=checkVar)
I think you can write box.deselect() to deselect it.
Edit: Oops, I just tested it, it's not working with ttk... Sorry. :)
I'm new to Tkinter, and also new to this forum. I am trying to learn to use Tkinter, and I have a problem!
I want to save some text to a text file by writing the text and then press a button to run a function that saves the info. But it seems like my "command" does not start the function.
def ny_artikel():
artikel_data = open ("artikel_databas.txt", "w")
artikel_data.write(ny_artikel.get())
artikel_data.close ()
spara_artikel = Button(new_product_window, text ="Save new article", command = ny_artikel)
spara_artikel.grid(row=7, column=1)
ny_artikel is an entry box used in my program, but I think it's too many rows to paste it all in here.
When I press the button, nothing at all happens. Not even an error message.
I assume, that the code in your answer is only part of your python file. I tried it out with Entry e in my example and it works right:
import tkinter
def ny_artikel():
with open('artikel_databas.txt', 'w') as artikel_data:
artikel_data.write(e.get())
main = tkinter.Tk()
e = tkinter.Entry(main)
e.grid(row=0, column=0)
spara_artikel = tkinter.Button(main, text ="Save new article", command = ny_artikel)
spara_artikel.grid(row=1, column=0)
main.mainloop()
As alternative I used 'with' 'as' in ny_artikel() function, which automatically closes the file. Using file.close() works fine as well.
What is the python keyword "with" used for?
Beginner programmer here, working on making a basic GUI as part of a tutorial I was following online, but none of them say how to get a Text box to update using the output of the other parts of your code.
I tried multiple other answers on the site, including one using StringVar's, which got me nowhere, another using a decorator, and the rest seemed way out of my depth.
Here's my code:
import tkinter as tk
import time
#Creating Root
root = tk.Tk()
#GUI TEMPLATE
frame =tk.Frame(root,
height = 100,
width = 400)
frame.pack()
v = StringVar()
colour = ["red","blue","green","white","yellow"]
labels = range(5)
#change number to change how many labels
for i in range(5):
l= tk.Label(root,
text = colour[i],
bg = colour[i])
l.place(x = 10 +i*70, y = 10, width=60, height=25)
T1 = tk.Text(root, height=2, width=40)
words = "Don't name your files after module names!"
T1.insert(tk.END, textvariable=v)
T1.place(x = 10, y= 40)
S = tk.Scrollbar(root)
S.config(command=T1.yview)
S.place(x = 340, y=40)
T1.config(yscrollcommand=S.set)
root.mainloop()
v.set("Something Else!")
Now, what it should output is a row of coloured labels, which works fine, and a text box with a scroll bar, which should instantly update to read 'Something Else!', which does not work fine.
Instead I get the following error:
NameError: name 'StringVar' is not defined
I know what this error means, it's just I've hit a wall when it comes to finding a solution that works for me, and doesn't need a doctorate to understand.
What I'm asking for is if someone can give me a solution that would work for this, and hopefully explain it!
Thanks in advance.
EDIT:
So after fixing the syntax error, and then finding out what I'm trying to do doesn't work, how would I go about this?
Could I use a label instead? Or is there another, better way?
Thanks again!
StringVar should accessed via tk:
v = tk.StringVar()
On another note, tk.Text.insert does not take a textvariable parameter, so the following won't work:
T1.insert(tk.END, textvariable=v)
# ^^^^^^^^^^^^??
From the docs:
Unlike for example the entry widget, text widgets don't support a
"textvariable" configuration option
Also see How can I connect a StringVar to a Text widget in Python/Tkinter? as to why this won't work.
I was experimenting with learning threading with Tkinter and have made something work to insert and update text in a Tkinter text box in Python3 which may be of help.
I found that by deleting the text I could then insert new text. Untill I deleted the existing text the instruction to insert text seamed to be ignored.
{ foo = input("Give me input: " )
self.T.delete("1.0", END) #Clear the text window so we can write.
self.T.insert(END,foo) #Write the new text.}
This is where the error is found:
global backbuttonimg
backbuttonimg = PhotoImage(file="backbutton.gif")
C6 = tkinter.Button(W_CheckDates, image=backbuttonimg, command = CheckDatesBack)
C6.pack()
I don't understand why this isn't working. I have another image in my program here:
def Login():
global W_Menu
W_Menu = Tk()
W_Menu.geometry('160x310+600+200')
W_Menu.title("NSS DB")
A0 = Canvas(W_Menu, width='160', height='160')
A0.pack()
global img
img = PhotoImage(file="nsslogo.gif")
A0.create_image(80,80, image=img)
I also get a similar error when I try to call the above definition after it has already been initially called (for example when my program logs out) so I have readjusted so the window simply deiconifies instead of calling it again, and I do not get the error again. However I am confused as to why I get an error with the former section of code now, as the button simply does not show up whether it is called for the first time or not. Sorry if this is a bit vague, please ask if I have not explained in enough detail. Thanks in advance.
P.S. I have looked in other threads with similar problems but none apply to me.
Ok so you say that the login function works once, then it can't work again. Here the problem can be solved using tk.Toplevel() instead of tk.Tk() see: why python photoimages don't exist? and tkinter.TclError: image "pyimage3" doesn't exist
These threads mention how you can't have two instances of Tk() running simultaneously, you have to use Toplevel() instead.
Why did these threads not apply to you (i think they do...)? But just a tip, if you state that they don't apply to you, then give reasons why, it helps make your question clearer. Also, add the full traceback when your question is about a particular error.
Hope this helps a bit.
Adding this for anyone who has tried the above with no success. If you have an erroneous path when running the script in some environments the path to the file is retained. I commented out everything from where I first use PhotoImage up to the window mainloop, run the script, close resulting gui, uncomment the code, run, and it shows the image as expected.
You can add a master parameter
backbuttonimg = PhotoImage(file="backbutton.gif",master=W_Menu)
Here Cledia
I am also facing this error.
And when I use Toplevel instead of To, it is working well
I suggest you to use TopleToplevel
Use tk.Toplevel() instead of tk.Tk() So its will work because the python coding library tkinter it doesnt make any sens if you make two windows working sametime with the tk.Tk() so you can use the toplevel() to open multiwindows in the same time !!
Hope it's Helpful