TKinter update scrolledText - python

On create window, tkinter.scrolledtext look up for SearchIP() to insert but never update layout. When I have different input for SearchIP() method never show up. It only shows the initial value.
Note: I added a couple of print statements to watch if SearchIP() returns the right thing, which is the case, for example for 'www.google.com' it prints '216.58.192.4', but this value never shows up in ScrolledText.
import socket
try:
# for Python2
import Tkinter as tk
import ScrolledText as tkst
except ImportError:
# for Python3
import tkinter as tk
import tkinter.scrolledtext as tkst
global Filename
root = tk.Tk()
frame = tk.Frame(root,width=100)
label = Label(root,text="http://")
label.grid(row=0,column=0)
entryVar = tk.StringVar()
entry = Entry(root,width=50,textvariable=entryVar)
entry.grid(row='0',column='1',columnspan=8)
def SearchIP():
print(entryVar.get())
add = str(entryVar.get())
ip_a = socket.gethostbyname(add)
print(str(ip_a))
return str(ip_a);
button1 = Button(root, text="Search IP", command=SearchIP)
button1.grid(row=1, column=0)
button2 = Button(root, text="DNS Recon")
button2.grid(row=1, column=1)
button3 = Button(root, text="Port Scanner")
button3.grid(row=1, column=2)
button4 = Button(root, text="Web Crawl")
button4.grid(row=1, column=3)
button5 = Button(root, text="Email Gathering")
button5.grid(row=1, column=4)
frame.grid(row=2, column=0, columnspan=30, rowspan=30)
edit_space = tkst.ScrolledText(
master = frame,
wrap = 'word', # wrap text at full words only
width = 45, # characters
height = 10, # text lines
# background color of edit area
)
# the padx/pady space will form a frame
edit_space.pack(fill='both', expand=True, padx=8, pady=8)
root.title("E-Z Security Audting")
root.resizable(True, True)
edit_space.insert('insert', SearchIP())
root.mainloop()
I also tried to modify the SearchIP() method and remove the return statement, but then I got this error:
File "C:/Users/kero/Desktop/Robert_HodgesKeroles_Hakeem_secproject/secproject/new‌​User_Interface.py", line 50, in <module>
edit_space.insert('insert', SearchIP())
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2986, in insert
self.tk.call((self._w, 'insert', index, chars) + args)`
SearchIP after modification
def SearchIP():
add = str(entryVar.get())
ip_a= socket.gethostbyname(add)

You have to use insert() inside SearchIP()
import socket
try:
# for Python2
import Tkinter as tk
import ScrolledText as tkst
except ImportError:
# for Python3
import tkinter as tk
import tkinter.scrolledtext as tkst
# --- functions ---
def SearchIP():
add = entryVar.get()
ip_a = socket.gethostbyname(add)
edit_space.insert('insert', ip_a + "\n")
# --- main ---
root = tk.Tk()
root.title("E-Z Security Audting")
root.resizable(True, True)
frame = tk.Frame(root, width=100)
frame.grid(row=2, column=0, columnspan=30, rowspan=30)
label = tk.Label(root, text="http://")
label.grid(row=0, column=0)
entryVar = tk.StringVar()
entry = tk.Entry(root, width=50, textvariable=entryVar)
entry.grid(row=0, column=1, columnspan=8)
button1 = tk.Button(root, text="Search IP", command=SearchIP)
button1.grid(row=1, column=0)
button2 = tk.Button(root, text="DNS Recon")
button2.grid(row=1, column=1)
button3 = tk.Button(root, text="Port Scanner")
button3.grid(row=1, column=2)
button4 = tk.Button(root, text="Web Crawl")
button4.grid(row=1, column=3)
button5 = tk.Button(root, text="Email Gathering")
button5.grid(row=1, column=4)
edit_space = tkst.ScrolledText(
master=frame,
wrap='word', # wrap text at full words only
width=45, # characters
height=10, # text lines
# background color of edit area
)
edit_space.pack(fill='both', expand=True, padx=8, pady=8)
root.mainloop()

Related

File-path not displaying on tkinter entry box

I've looked into multiple threads and I can't seem to find out why the stringVar is not displaying on the entry box, once the file has been selected. I can see the print(filePath.get()) has the selected file but entry box stays blank.
Can someone please help?
#import libraries
import os
import paramiko
import tkinter as tk
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from tkinter.filedialog import askopenfilename
import sys
def getFilePath():
file_selected = filedialog.askopenfilename(title="Select File")
filePath.set(file_selected)
print(filePath.get())
#-------------------------------GUI------------------------------
gui = tk.Tk()
gui.title("NetWizard 1.0")
tabControl = ttk.Notebook(gui, width = 1000, height = 700)
gui.resizable(False, False)
tabControl.pack( expand = 1, fill ="both")
tab1 = ttk.Frame(tabControl)
tab2 = ttk.Frame(tabControl)
tabControl.add(tab1, text ='RUGGEDCOM', )
tabControl.add(tab2, text ='WESTERMO')
#--------------------------RUGGEDCOM WIDGET----------------------
filePath = StringVar()
entry1 = Entry(gui, textvariable=filePath, width=1000)
entry1 = tk.Entry(tab1, width = 60)
label1 = Label(tab1, text="IP Address List Input:", font=('Arial', '11', 'bold'))
button1 = Button(tab1, text='Browse', command=getFilePath, font=("Arial", 10), width = 10, bg='#add8e6')
label1.grid(row=0, column=0, padx = 15, pady=10, sticky = W)
entry1.grid(row=1, column=0, padx=15, pady=5)
button1.grid(row=1, column=1, padx=0, pady=0, sticky = W)
gui.mainloop()
It is because you did not set your entry text value in your function. Supposedly after you get your file path, you should assign the file path text value into the entry.
You can do so by inserting this code into your codes.
entry1.insert(0, "Test Value")
Your codes:
def getFilePath():
file_selected = filedialog.askopenfilename(title="Select File")
filePath.set(file_selected)
print(filePath.get())
entry1.insert(0, filePath.get())
You can refer to https://www.geeksforgeeks.org/how-to-set-the-default-text-of-tkinter-entry-widget/ .

Why isn't this label changing when I use the tkinter config option

I want to change this label so I used the widget.config option but it's not working for some reason. Here's my code:
import tkinter as tk
root = tk.Tk()
root.attributes('-fullscreen', True)
exit_button = tk.Button(root, text="Exit", command = root.destroy)
exit_button.place(x=1506, y=0)
def answer():
global main_entry
answer_label.config(main_entry)
frame = tk.Frame(root)
main_entry = tk.Entry(frame, width=100)
main_entry.grid(row=0, column=0)
go_button = tk.Button(frame, text='Go!', width=85, command = answer)
go_button.grid(row=1, column=0)
answer_label = tk.Label(text = "Hey").pack()
frame.place(relx=.5, rely=.5, anchor='center')
root.mainloop()
While using the config function, you have to mention what is it that you want to change.
try:-
answer_label.config(text="Text that you want to be displayed")
Also you have not fetched the value from the Entry widget: For that, you can use:
answer_label.config(text=main_entry.get())
Full code will look like this:
import tkinter as tk
root = tk.Tk()
root.attributes('-fullscreen', True)
exit_button = tk.Button(root, text="Exit", command = root.destroy)
exit_button.place(x=1506, y=0)
def answer():
answer_label.config(text=main_entry.get())
frame = tk.Frame(root)
main_entry = tk.Entry(frame, width=100)
main_entry.grid(row=0, column=0)
go_button = tk.Button(frame, text='Go!', width=85, command = answer)
go_button.grid(row=1, column=0)
answer_label = tk.Label(text = "Hey")
answer_label.pack()
frame.place(relx=.5, rely=.5, anchor='center')
root.mainloop()
This code works. It configures the label to change the text. I don't know what you are trying to achieve with the config(main_entry).
import tkinter as tk
root = tk.Tk()
root.attributes('-fullscreen', True)
exit_button = tk.Button(root, text="Exit", command = root.destroy)
exit_button.place(x=1506, y=0)
def answer():
global main_entry, answer_label
answer_label.config(text="hi")
frame = tk.Frame(root)
main_entry = tk.Entry(frame, width=100)
main_entry.grid(row=0, column=0)
go_button = tk.Button(frame, text='Go!', width=85, command = answer)
go_button.grid(row=1, column=0)
answer_label = tk.Label(text = "Hey")
answer_label.pack()
frame.place(relx=.5, rely=.5, anchor='center')
root.mainloop()

Tkinter Texboxes Becoming Transparent On A Button Click

from tkinter import Tk, scrolledtext, INSERT, Button, PhotoImage, Label, Text
root = Tk()
root.geometry('1000x550')
bg = PhotoImage(file='./assets/bg.png')
root.resizable(False, False)
root.title('Testing Classes')
window = Label(root, image=bg)
window.place(relheight=1, relwidth=1)
tokenBox = Text(window, bd=0, height=1, width=30)
def get_token():
token = tokenBox.get('1.0', 'end-1c')
print(token)
return token
prefixBox = Text(window, bd=0, height=1, width=20)
prefixBox.place(x=400, y=100)
tokenBox.place(x=350, y=150)
def get_prefix():
prefix = prefixBox.get('1.0', 'end-1c')
print(prefix)
return prefix
def codeInsert():
prefixBox.place(x=400, y=100)
tokenBox.place(x=350, y=150)
code = f'''
import discord
from discord.ext import commands
bot = commands.Bot(prefix={get_prefix()})
bot.run({get_token()})
'''
codeBox = scrolledtext.ScrolledText(window, width=50, height=15, bg='Black', fg='Red')
codeBox.insert(INSERT, code)
codeBox.configure(state='disabled')
codeBox.place(x=300, y=300)
submit = Button(window, command=codeInsert, text='Submit Everything', bd=0)
submit.place(x=425, y=250)
window.mainloop()  
When I click on the Submit button, it hides all the textboxes. The textboxes only comes back, when I click on them. I still see the cursor change, when I hover on them, but the Labels also get hidden, and they are never shows back. It's like they become transparent, and become opaque only when I click on them.
Try this:
from tkinter import Tk, scrolledtext, INSERT, Button, PhotoImage, Label, Text
root = Tk()
root.geometry('1000x550')
bg = PhotoImage(file='./assets/bg.png')
root.resizable(False, False)
root.title('Testing Classes')
window = Label(root, image=bg)
window.place(relheight=1, relwidth=1)
tokenBox = Text(root, bd=0, height=1, width=30)
def get_token():
token = tokenBox.get('1.0', 'end-1c')
print(token)
return token
prefixBox = Text(root, bd=0, height=1, width=20)
prefixBox.place(x=400, y=100)
tokenBox.place(x=350, y=150)
def get_prefix():
prefix = prefixBox.get('1.0', 'end-1c')
print(prefix)
return prefix
def codeInsert():
# Add these lines here if you want to remove the button/entries.
#tokenBox.place_forget()
#prefixBox.place_forget()
#submit.place_forget()
code = f'''
import discord
from discord.ext import commands
bot = commands.Bot(prefix={get_prefix()})
bot.run({get_token()})
'''
codeBox = scrolledtext.ScrolledText(root, width=50, height=15, bg='Black', fg='Red')
codeBox.insert(INSERT, code)
codeBox.configure(state='disabled')
codeBox.place(x=300, y=300)
submit = Button(root, command=codeInsert, text='Submit Everything', bd=0)
submit.place(x=425, y=250)
window.mainloop()
The problem is that you had the label (named window) as the master for the other widgets. You should never have a tk.Label as a parent for anything. When I changed all their parents to root it worked.

The button text and color does not appear on Tkinter program

The program works fine on other computers that run window's or ubuntu but the text won't show on the buttons. Here's a snippet of a longer code:
from Tkinter import *
from math import atan
from math import log
import math
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
# --- function ---
def create_first_frame():
global root
global frame
# frame.destroy()
frame = Frame(bg="blue")
frame.pack()
label1 = Label(frame, text="hello!", fg="white", bg="blue", font="normal 30")
label1.pack()
button1 = Button(frame, text="Enter", fg="white", bg="blue", font="normal 20")
button1.pack()
button2 = Button(frame, text="Exit", font="normal", fg="white", bg="red", command=root.destroy)
button2.pack(side=LEFT)
root = Tk()
create_first_frame()
root.mainloop()
We were expecting words like "start" and "exit" to show.
We just want the colors of the buttons to show along with the text
I am not sure what the question is. I took out unnecessary imports, and ran
from tkinter import *
def create_first_frame():
global root
global frame
# frame.destroy()
frame = Frame(bg="blue")
frame.pack()
label1 = Label(frame, text="hello!", fg="white", bg="blue", font="normal 30")
label1.pack()
button1 = Button(frame, text="Enter", fg="white", bg="blue", font="normal 20")
button1.pack()
button2 = Button(frame, text="Exit", font="normal", fg="white", bg="red", command=root.destroy)
button2.pack(side=LEFT)
root = Tk()
create_first_frame()
root.mainloop()
Which creates for me:
This is colored as you have specified...

What can I do to make the frame without widgets?

What I want the frame to do is that when I click on the 'clear' button, the frame is cleaned but it does not and when I enter a string that is not valid and then a valid one, it shows traces of the past and past action. I already tried changing the Label.grid () by a Label.pack (), but it is worse since the 'animation' looks like a stack without removing any element when the 'clear' button is pressed
This is basically what would make it change
from tkinter import *
import tkinter.ttk as ttk
def clear():
area.delete(0,END)
frame.config(bd=1, relief=SUNKEN)
frame.update()
status = Label(frame)
status.grid(row=0, column=0, sticky=NSEW)
def statusVal(value):
if not value == 0:
status = Label(frame, background="#ff4242", fg="#262626", text="Cadena invalida", anchor="center")
status.grid(row=0, column=0)
frame.config(bd=1, relief=SUNKEN, background="#ff4242")
frame.update()
else:
status = Label(frame, background="#56ed42", fg="#262626", text="Cadena valida", anchor="center")
status.grid(row=0, column=0)
frame.config(bd=1, relief=SUNKEN, background="#56ed42")
frame.update()
#Test
def validation():
capture = area.get()
if capture == '1':
return statusVal(0)
else:
return statusVal(1)
root = Tk()
root.geometry("300x150+300+300")
area = Entry(root)
area.grid(row=1, column=0, columnspan=2, sticky=E+W+S+N, padx=5)
frame = Frame(root, bd=1, relief=SUNKEN)
frame.grid(row=2, column=0, padx=5, pady=5, columnspan=2, sticky=W+E+S+N)
frame.columnconfigure(0,weight=5)
frame.rowconfigure(0,weight=5)
abtn = Button(root, text="Validate", command=validation)
abtn.grid(row=1, column=3)
cbtn = Button(root, text="Clear", command=clear)
cbtn.grid(row=2, column=3, pady=5)
root.mainloop()
See if this works better. The main change was to have the status Label always exist and hide or unhide it as desired — instead of creating a new one every time the validation() function was called. I also removed the code that was explicitly updating the frame which isn't necessary.
from tkinter import *
import tkinter.ttk as ttk
def clear():
area.delete(0,END)
status.grid_remove() # Hide. but remember grid options.
def statusVal(value):
if not value == 0:
status.config(background="#ff4242", fg="#262626", text="Cadena invalida",
anchor="center")
status.grid() # Unhide
else:
status.config(background="#56ed42", fg="#262626", text="Cadena valida",
anchor="center")
status.grid() # Unhide
#Test
def validation():
capture = area.get()
if capture == '1':
statusVal(0)
else:
statusVal(1)
# Main
root = Tk()
root.geometry("300x150+300+300")
area = Entry(root)
area.grid(row=1, column=0, columnspan=2, sticky=E+W+S+N, padx=5)
frame = Frame(root, bd=1, relief=SUNKEN)
frame.grid(row=2, column=0, padx=5, pady=5, columnspan=2, sticky=W+E+S+N)
frame.columnconfigure(0,weight=5)
frame.rowconfigure(0,weight=5)
# Initialize status Label.
status = Label(frame, anchor="center")
status.grid(row=0, column=0)
status.grid_remove() # Hide it.
abtn = Button(root, text="Validate", command=validation)
abtn.grid(row=1, column=3)
cbtn = Button(root, text="Clear", command=clear)
cbtn.grid(row=2, column=3, pady=5)
root.mainloop()

Categories

Resources