Why did my tkinter text entry disappear suddenly? - python

So I want this Tkinter entry but I can't find it what am I doing wrong?
import tkinter as tk
import math
import time
root = tk.Tk()
root.geometry()
root.attributes("-fullscreen", True)
exit_button = tk.Button(root, text = "Exit", command = root.destroy)
exit_button.place(x=1506, y=0)
frame = tk.Frame(root)
main_entry = tk.Entry(root, width = 100, fg = "black").place()
frame.place(relx=.5,rely=.5, anchor='center')
root.mainloop()
I want to make the entry show, why is gone I did everything right?

here, put layout options on variables like this (if You want to just place a widget somewhere use .pack which also does not necessarily require arguments):
import tkinter as tk
import math
import time
root = tk.Tk()
root.geometry()
root.attributes("-fullscreen", True)
exit_button = tk.Button(root, text = "Exit", command = root.destroy)
exit_button.place(x=1506, y=0)
frame = tk.Frame(root)
main_entry = tk.Entry(root, width = 100, fg = "black")
main_entry.place(x=50, y=50)
frame.place(relx=.5,rely=.5, anchor='center')
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/ .

How do i open a new window on the click of a button with tkinter?

I am making a text base game and would like to get a new root window on the click of a button while simultaneously closing the original one?
#Packages
import tkinter as tk
from tkinter import ttk, BOTTOM
from PIL import ImageTk, Image
#Create Window
root = tk.Tk()
root.geometry("360x740")
root.resizable(True, True)
root.title("Box")
root.configure(bg="black")
#Load Image
canvas = tk.Canvas(root, width=360, height=360, bg="black")
tk.Canvas(bg="black")
canvas.pack()
img = ImageTk.PhotoImage(Image.open("sample_image.jpg"))
canvas.create_image(180, 200, image=img)
#Create Text Widget
T = tk.Text(root, height=10, width=52, bg="black", fg="white")
l = tk.Label(root, text="Hard Living by Amsha", fg="white", bg="black")
l.config(font=(None, 14,))
story = """
"""
l.pack()
T.pack()
#Continue Button
yes_button = ttk.Button(root, text="Continue", command=lambda: next_window())
yes_button.pack(pady=75, side=BOTTOM)
T.insert(tk.END, story)
root.mainloop()
def next_window():
root.quit()
root = tk.Tk()
root.geometry("360x740")
root.resizable(True, True)
root.title("Hard Living")
root.configure(bg="black")
root.mainloop()

Tkinter StringVar shows random numbers instead of variable on label

The title says it all.
import tkinter as tk
from tkinter import filedialog, Text, StringVar
root = tk.Tk()
root.resizable(False, False)
var1 = StringVar()
topn = ""
print(topn)
def addApp():
global topn
topn = topn + "1"
print(topn)
var1.set(topn)
canvas = tk.Canvas(root, height=500, width=400, bg="#263D42")
canvas.pack()
frame = tk.Frame(root, bg="orange")
frame.place(relwidth=0.8, relheight=0.8, relx=0.1, rely=0.1)
Resultlabel = tk.Label(root,text=var1.get, padx=100, pady=5, fg="white", bg="grey")
Resultlabel.pack()
t
openFile = tk.Button(frame, text="Open File", padx=10, pady=5, fg="white", bg="#263D42", command=addApp)
openFile.pack()
root.mainloop()
[btw, ignore the Open File, i was following a tutorial to learn the basics of tkinter and then edited the code to fit my needs. I forgot to change the text in the button.]
https://i.stack.imgur.com/2FVKG.png
text is not the same thing as textvariable. text is used to associate static text with a widget, textvariable is used to have the text of the widget be synchronized with the value of a variable.
If you want ResultLabel to always show what is in var1 you must define it like this:
Resultlabel = tk.Label(root,textvariable=var1, padx=100, pady=5, fg="white", bg="grey")

How to change colour in tkinter progressbar

I am using tkiner mesagebox and ttk progressbar with Python 3. I try to set a text window in one line and a progressbar in the next line. This is working so far, but I am not able to change the colour from green (default) to another value. With this post How to change ttk.progressBar color in python I was able to turn the colour to black, but then I don't know how to get the text over it. Can someone help me?
from tkinter import *
from tkinter import messagebox
from tkinter import ttk
#bar in green with text
root = Tk()
gpw_l1 = Label(root, text="This should be a black bar")
gpw_l2 = ttk.Progressbar(root, orient="horizontal", length=500, mode="determinate")
gpw_l2.grid(row=2, column=0, pady=10)
gpw_l2["maximum"] = 1.0
x = 0.7
gpw_l2["value"] = x
gpw_l1.grid(row=0, columnspan=2)
gpw_l2.grid(row=1, columnspan=2)
root.geometry('+100+200')
root.mainloop()
root.quit()
#bar in red, but no text
root2 = Tk()
frame = Frame(root2)
frame.grid()
s = ttk.Style()
s.theme_use('clam')
s.configure("red.Horizontal.TProgressbar", foreground='red', background='black')
ttk.Progressbar(frame, style="red.Horizontal.TProgressbar", orient="horizontal", length=600, mode="determinate",
maximum=4, value=1).grid(row=1, column=1)
frame.pack()
root2.mainloop()
root2.quit()
from tkinter import *
from tkinter import messagebox
from tkinter import ttk
#bar in red, but no text
root2 = Tk()
frame = Frame(root2)
frame.grid()
s = ttk.Style()
s.theme_use('clam')
s.configure("red.Horizontal.TProgressbar", foreground='red', background='black')
gpw_l1 = Label(frame, text="This should be a black bar").grid(row=1, column=1)
ttk.Progressbar(frame, style="red.Horizontal.TProgressbar", orient="horizontal", length=600, mode="determinate",
maximum=4, value=1).grid(row=2, column=1)
frame.pack()
root2.mainloop()
root2.quit()

TKinter update scrolledText

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()

Categories

Resources