How can I do a response = requests.get() with variable from tkinter, like this
so that I can send some bumbersto an api like 20 21 22 and the request looks like
"https://google.com/202122"
def d():
response = requests.get("myserverip/send.php?host=e2<eq>#%s#&port=e2<eq>#%s#&time=e3<eq>#%s#&method=udp") % (e1, e2, e3)
print(response)
#call
def d1():
threading.Thread(target=d).start()
def window():
window = Tk()
window.title("tools")
window.geometry("530x240")
Label(window, text="1").grid(row=0)
Label(window, text="2").grid(row=1)
Label(window, text="3").grid(row=2)
e1 = Entry(window)
e2 = Entry(window)
e3 = Entry(window)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
e3.grid(row=2, column=1)
Button(window, text='DOREQUEST', bg="gray", fg="white", command=d1).grid(row=3, column=1, sticky=E, pady=30)
frame_label.place(x=210, y=0)
window.mainloop()
#THREADING
p = Process(target=window)
p.start()
p.join()
def d():
a = e1.get()
b = e2.get()
c = e3.get()
print(a, b, c)
request = requests.get(f"http://google.com/{a}{b}{c}".format(a, b, c))
print(request)
#call
def d1():
threading.Thread(target=d).start()
def window():
window = Tk()
window.title("tools")
window.geometry("530x240")
Label(window, text="1").grid(row=0)
Label(window, text="2").grid(row=1)
Label(window, text="3").grid(row=2)
global e1, e2, e3
e1 = Entry(window)
e2 = Entry(window)
e3 = Entry(window)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
e3.grid(row=2, column=1)
Button(window, text='DOREQUEST', bg="gray", fg="white", command=d1).grid(row=3, column=1, sticky=E, pady=30)
frame_label.place(x=210, y=0)
window.mainloop()
#THREADING
p = Process(target=window)
p.start()
p.join()
Related
I'm trying to write an app that computes a figure after compound interest over time, but one of my entry boxes isn't showing up. The get function appears to work but the second entry box won't display:
from tkinter import *
from tkinter import ttk
def main():
root = Tk()
root.geometry("600x200")
#frm = ttk.Frame(root, padding=100)
e1 = ttk.Entry(root, width=20, rowspan=1)
e2 = ttk.Entry(root, width=20, rowspan=1)
e3 = ttk.Entry(root, width=20, rowspan=1)
e11 = e1.get()
e22 = e2.get()
e33 = e3.get()
e1.grid(column=1, row=0)
e2.grid(column=1, row=1)
e2.grid(column=1, row=2)
Label(root, text="Amount to invest:").grid(column=0, row=0)
Label(root, text="Number of years invested:").grid(column=0, row=1)
Label(root, text="Add yearly return in percent:").grid(column=0, row=2)
submit = Button(root, text="Submit", command=lambda: Label(root, int(e11)*(int(e22)**int(e33))), padx=10, bg="cyan", fg="Red")
submit.grid(column=1, row=3)
enter code here
root.mainloop()
if __name__ == '__main__':
main()
Your code works, but I removed the rowspan parameter
e1 = ttk.Entry(root, width=20, )
e2 = ttk.Entry(root, width=20, )
e3 = ttk.Entry(root, width=20, )
e11 = e1.get()
e22 = e2.get()
e33 = e3.get()
e1.grid(column=1, row=0)
e2.grid(column=1, row=1)
e3.grid(column=1, row=2)
Hey i tryed to make a canvas in tkinter and make the entry to a global variable but i get the error (AttributeError: 'int' object has no attribute 'get')
def a():
a1 = e1.get()
b1 = e2.get()
c1 = e3.get()
print(a, b, c)
def window():
window = Tk()
my_canvas = Canvas(window, width=530, height=240, bd=0, highlightthickness=0)
my_canvas.pack(fill="both", expand=True)
global e1, e2, e3
e1 = Entry(window, fg="black", bd=0)
e2 = Entry(window, fg="black", bd=0)
e3 = Entry(window, fg="black", bd=0)
e1 = my_canvas.create_window(40, 20, anchor="nw", window=e1)
e2 = my_canvas.create_window(40, 60, anchor="nw", window=e2)
e3 = my_canvas.create_window(40, 100, anchor="nw", window=e3)
button1 = Button(window, text='Print', bd=0, bg="black", fg="white", command=a)
button1 = my_canvas.create_window(400, 20, anchor="nw", window=button1)
window.mainloop()
p = Process(target=window)
p.start()
p.join()
You are calling get on .create_window object
You should not run over the Entry objects:
from tkinter import Tk, Canvas, Button, Entry
from multiprocessing import Process
def a():
a = e1.get()
b = e2.get()
c = e3.get()
print(a, b, c)
def window():
window = Tk()
my_canvas = Canvas(window, width=530, height=240, bd=0, highlightthickness=0)
my_canvas.pack(fill="both", expand=True)
global e1, e2, e3
e1 = Entry(window, fg="black", bd=0)
e2 = Entry(window, fg="black", bd=0)
e3 = Entry(window, fg="black", bd=0)
e1_w = my_canvas.create_window(40, 20, anchor="nw", window=e1)
e2_w = my_canvas.create_window(40, 60, anchor="nw", window=e2)
e3_w = my_canvas.create_window(40, 100, anchor="nw", window=e3)
button1 = Button(window, text='Print', bd=0, bg="black", fg="white", command=a)
button1 = my_canvas.create_window(400, 20, anchor="nw", window=button1)
window.mainloop()
p = Process(target=window)
p.start()
p.join()
First, when the number of rectangles is entered less than 24 it works fine. When I put the number of rectangles more than 24, the entry boxes exceed the window height and I cannot see it. I want to use a scroll bar at the right side when the number of entry boxes exceeds the window height. The documentation implies that only the List, Textbox, Canvas and Entry widgets support the scrollbar interface. So, how can I do for gridboxes?
Note : The code is verifiable.
from tkinter import *
temp_recta = []
def save_content(top):
for j in range(0, int(rectangles.get()) * 2):
temp_recta.append(rectangle_values[j].get())
root = Toplevel()
l1 = Label(root, text="Size of Sheet[Height]:")
l1.grid(row=0, column=0)
l2 = Label(root, text="Size of Sheet[Width]:")
l2.grid(row=1, column=0)
height = StringVar()
e1 = Entry(root, textvariable=height)
e1.grid(row=0, column=1)
width = StringVar()
e2 = Entry(root, textvariable=width)
e2.grid(row=1, column=1)
b5 = Button(root, text="Proceed", width=12, command=lambda: save_sheetsize(e1, e2, root))
b5.grid(row=2, column=0)
b6 = Button(root, text="Back", width=12, command=lambda: (root.destroy(), top.deiconify()))
b6.grid(row=2, column=1)
root.title("Sheet Size")
def save_sheetsize(e1, e2, root):
global x
global y
x = float(e2.get())
y = float(e1.get())
root.destroy()
window.destroy()
rectangle_values = []
x = 0
y = 0
k = 0
def open_window():
window.withdraw()
global k
top = Toplevel()
top.title("Rectangles")
for i in range(0, int(rectangles.get()) * 2):
if (i % 2) == 0:
l4 = Label(top, text="Size of rectangle:")
l4.grid(row=i, column=0)
rectangle_values.append(StringVar())
en = Entry(top, textvariable=rectangle_values[i])
en.grid(row=i, column=1)
b3 = Button(top, text="Save", width=12, command=lambda: (top.withdraw(), save_content(top)))
b3.grid(row=int(rectangles.get()) * 2 + 1, column=0)
b4 = Button(top, text="Back", width=12, command=lambda: (top.destroy(), window.deiconify()))
b4.grid(row=int(rectangles.get()) * 2 + 1, column=1)
k = int(rectangles.get())
window = Tk()
l3 = Label(window, text="Number of Rectangles:")
l3.grid(row=0, column=0)
rectangles = StringVar()
e3 = Entry(window, textvariable=rectangles)
e3.grid(row=0, column=1)
b1 = Button(window, text='Submit', width=12, command=open_window)
b1.grid(row=3, column=1)
window.title("Rectangle Configuration")
window.mainloop()
Here is a simple class to do this. Add your text widget and it will give you a scrollbar when your window is full of text.
import tkinter
class Scrollbar:
def __init__(self,text):
self.frame = text.master
self.text = text
self.text.configure(wrap='none')
self.for_x_view()
self.for_y_view()
def for_x_view(self):
# scroll Bar x For width
scroll_x=tkinter.Scrollbar(self.frame, orient='horizontal',command=self.text.xview)
scroll_x.config(command=self.text.xview)
self.text.configure(xscrollcommand=scroll_x.set)
scroll_x.pack(side='bottom', fill='x', anchor='w')
return
def for_y_view(self):
# Scroll Bar y For Height
scroll_y = tkinter.Scrollbar(self.frame)
scroll_y.config(command=self.text.yview)
self.text.configure(yscrollcommand=scroll_y.set)
scroll_y.pack(side='right', fill='y')
return
if __name__ == '__main__':
root = tkinter.Tk()
pad = tkinter.Text(root,wrap='none')
Scrollbar(pad)
pad.pack()
root.mainloop()
from tkinter import *
# global new_width
# global new_height
# global new_rectangles
rectangle_values=[]
x = 0
y = 0
k = 0
def open_window():
global k
top=Toplevel()
top.title("Rectangles")
top.geometry("300x600")
for i in range(0,int(rectangles.get())*2):
if(i % 2) == 0:
l4=Label(top, text="Size of rectangle:")
l4.grid(row=i,column=0)
rectangle_values.append(StringVar())
en=Entry(top, textvariable=rectangle_values[i])
en.grid(row=i,column=1)
b3=Button(top, text="Save", width=12, command=save_content)
b3.grid(row=int(rectangles.get())*2+1,column=0)
b4=Button(top, text="Close", width=12, command=top.destroy)
b4.grid(row=int(rectangles.get())*2+1,column=1)
k = int(rectangles.get())
def save_sheetsize():
global x
global y
x = float(e2.get())
y = float(e1.get())
temp_recta=[]
def save_content():
for j in range(0,int(rectangles.get())*2):
temp_recta.append(rectangle_values[j].get())
window=Tk()
l3=Label(window, text="Number of Rectangles:")
l3.grid(row=0,column=0)
# defining entries
rectangles=StringVar()
e3=Entry(window, textvariable=rectangles)
e3.grid(row=0,column=1)
# Defining buttons
b1=Button(window, text='Submit', width=12, command=lambda:(window.destroy(),open_window()))
b1.grid(row=3,column=1)
window.title("Rectangle Configuration")
window.geometry("300x600")
window.mainloop()
root=Tk()
l1=Label(root, text="Size of Sheet[Height]:")
l1.grid(row=0,column=0)
l2=Label(root, text="Size of Sheet[Width]:")
l2.grid(row=1,column=0)
# defining entries
height=StringVar()
e1=Entry(root, textvariable=height)
e1.grid(row=0,column=1)
width=StringVar()
e2=Entry(root, textvariable=width)
e2.grid(row=1,column=1)
b5=Button(root, text="Save", width=12, command=save_sheetsize)
b5.grid(row=2,column=0)
b6=Button(root, text="Close", width=12, command=root.destroy)
b6.grid(row=2,column=1)
root.title("Sheet Size")
root.geometry("300x600")
root.mainloop()
When I run this code, I input a number at first and press the button which calls the function open_window and destroy, a window named tk pops up along with the window which is in open_window. When I close that tk window, the other window that I want also closes. I couldn't figure it out. How do I avoid that tk window from popping up?
This is just an example you can follow to fix your problem, and modify, update as you need.
from tkinter import *
# global new_width
# global new_height
# global new_rectangles
temp_recta = []
def save_content(top):
top.destroy()
for j in range(0, int(rectangles.get()) * 2):
temp_recta.append(rectangle_values[j].get())
root = Toplevel()
l1 = Label(root, text="Size of Sheet[Height]:")
l1.grid(row=0, column=0)
l2 = Label(root, text="Size of Sheet[Width]:")
l2.grid(row=1, column=0)
# defining entries
height = StringVar()
e1 = Entry(root, textvariable=height)
e1.grid(row=0, column=1)
width = StringVar()
e2 = Entry(root, textvariable=width)
e2.grid(row=1, column=1)
b5 = Button(root, text="Save", width=12, command=lambda: save_sheetsize(e1, e2, root))
b5.grid(row=2, column=0)
b6 = Button(root, text="Close", width=12, command=lambda: (root.destroy(), window.deiconify()))
b6.grid(row=2, column=1)
root.title("Sheet Size")
def save_sheetsize(e1, e2, root):
global x
global y
x = float(e2.get())
y = float(e1.get())
root.destroy()
window.deiconify()
rectangle_values = []
x = 0
y = 0
k = 0
def open_window():
window.withdraw()
global k
top = Toplevel()
top.title("Rectangles")
top.geometry("300x600")
for i in range(0, int(rectangles.get()) * 2):
if (i % 2) == 0:
l4 = Label(top, text="Size of rectangle:")
l4.grid(row=i, column=0)
rectangle_values.append(StringVar())
en = Entry(top, textvariable=rectangle_values[i])
en.grid(row=i, column=1)
b3 = Button(top, text="Save", width=12, command=lambda: save_content(top))
b3.grid(row=int(rectangles.get()) * 2 + 1, column=0)
b4 = Button(top, text="Close", width=12, command=lambda: (top.destroy(), window.deiconify()))
b4.grid(row=int(rectangles.get()) * 2 + 1, column=1)
k = int(rectangles.get())
window = Tk()
l3 = Label(window, text="Number of Rectangles:")
l3.grid(row=0, column=0)
# defining entries
rectangles = StringVar()
e3 = Entry(window, textvariable=rectangles)
e3.grid(row=0, column=1)
# Defining buttons
b1 = Button(window, text='Submit', width=12, command=open_window)
b1.grid(row=3, column=1)
window.title("Rectangle Configuration")
window.geometry("300x600")
window.mainloop()
I have written the following to allow users to be added,the script runs fine with no errros but no user account is created.
from tkinter import *
import subprocess
def add():
subprocess.call(['adduser', '-N', '-g', group.get(), '-c', fname.get(), '-d', '/home/oucu.get()'])
root = Tk()
oucu = StringVar()
e2 = Entry(root, textvariable=oucu).grid(row=0, column=1)
fname = StringVar()
e3 = Entry(root, textvariable=fname).grid(row=1, column=1)
sname = StringVar()
e4 = Entry(root, textvariable=sname).grid(row=2, column=1)
group = StringVar()
e6 = Entry(root, textvariable=group).grid(row=3, column=1)
b1 = Button(root, text='Next', command=add).grid(row=4, column=1)
Label(root, text="Users Oucu").grid(row=0, column=0, sticky=W)
Label(root, text="First Name").grid(row=1, column=0, sticky=W)
Label(root, text="Last Name").grid(row=2, column=0, sticky=W)
Label(root, text="Group").grid(row=3, column=0, sticky=W)
root.mainloop()