I'm trying to understand the following example below:
import sys
from tkinter import *
def btn():
login_frame.destroy()
home_frame = home()
home_frame.pack(fill="both", expand=True)
def btn2():
home_frame.destroy()
#home_frame = home()
#home_frame.pack(fill="both", expand=True)
def login():
frame = Frame(root)
Label(frame, text = "test1").grid(row = 0)
Label(frame, text = "test2").grid(row = 1)
Label(frame, text = "test3").grid(row = 2)
e1 = Entry(frame)
e2 = Entry(frame)
e1.grid(row=1, column = 1)
e2.grid(row=2, column = 1)
Button(frame, text = 'btn1', command = btn).grid(row = 4, column = 0, sticky = W, pady = 4)
Button(frame, text = 'btn2', command = btn).grid(row = 4, column = 1, sticky = W, pady = 4)
return frame
def home():
frame = Frame(root)
Label(frame, text="Welcome").pack()
Button(frame, text = 'test', command = btn2).pack()
return frame
root = Tk()
root.wm_title('test')
login_frame = login()
login_frame.pack(fill="both", expand=True)
root.mainloop()
I create the first login_frame, and btn1 destroys it and creates home_frame right? but when I click the next btn I get the following error:
NameError: name 'home_frame' is not defined
Why isn't this frame defined? I thought it would have been defined when btn is pushed and home_frame = home() ?
Related
Not really sure what im doing in this stage tbh, found someone asking a similar thing and tried to include it. However im not sure how to integrate it and at the moment they both do their own tkinter windows. One saving a .txt file, the other producing what ive written.
import sys
import tkinter as tk
from tkinter import *
from tkinter.messagebox import showinfo
root = tk.Tk()
root.wm_title('QR Code Generator')
def login():
frame = Frame(root)
Label(frame, text = "Welcome to QR Code Generator").grid(row = 0)
Label(frame, text = "Enter the link you want as a QR Code ").grid(row = 1)
e1 = Entry(frame)
e1.grid(row=1, column = 1)
Button(frame, text = 'Continue', command = save).grid(row = 4, column = 1, sticky = W, pady = 4)
return frame
def save():
file_name = entry.get()
with open(file_name + '.txt', 'w') as file_object:
file_object.write(file_name)
if __name__ == '__main__':
top = tk.Tk()
entry_field_variable = tk.StringVar()
entry = tk.Entry(top, textvariable=entry_field_variable)
entry.pack()
tk.Button(top, text="save", command=save).pack()
login_frame = login()
login_frame.pack(fill="both", expand=True)
root.mainloop()
wanting the "paste link for qr code" section to be saved into a .txt
Your filename is not defined anywhere in the code
I.e. your filename is empty, this results in a file with no name '.txt'
def save():
file_name = entry.get()
with open(file_name + '.txt', 'w') as file_object:
file_object.write(file_name)
Your code opens two windows, it is being created in this line, whenever you define another instance of Tk() it will be a new window, if you need it, I recommend Toplevel
if __name__ == '__main__':
top = tk.Tk()
Try something cleaner and easier to understand
import tkinter as tk
from tkinter import *
root = tk.Tk()
root.wm_title('QR Code Generator')
def save():
content = e1.get()
with open('your_file.txt', 'w') as file_object:
file_object.write(content)
frame = Frame(root)
frame.pack()
Label(frame, text="Welcome to QR Code Generator").pack()
Label(frame, text="Enter the link you want as a QR Code ").pack()
e1 = Entry(frame)
e1.pack()
Button(frame, text='Continue', command=save).pack()
if __name__ == '__main__':
root.mainloop()
The qrcode is created as a picture. You can write a picture as base64 encoded to a text file, but I recommend to save the picture as an image.
I understood your request as "create" and "show" the saved picture into a tkinter window. I saved the picture with a timestamp.
My prototype:
import tkinter as tk
import qrcode
from PIL import ImageTk, Image
import time
root = tk.Tk()
root.title('QR Code Generator')
root.geometry("450x420")
#root.state("zoomed") whole display size
root.config(bg="#2c3e50")
#root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
def create_QR(link_input):
print(link_input.get())
lnk = link_input.get()
global qr_img
qr = qrcode.QRCode(version=1, error_correction = qrcode.constants.ERROR_CORRECT_L, box_size=10, border=3)
qr.add_data(lnk)
qr.make(fit=True)
time_str = str(int(time.time()))
img = qr.make_image(fill_color='cyan', back_color='#2c3e50')
img.save(f'qrcode_{time_str}.png')
qr_img = f'qrcode_{time_str}.png'
return qr_img
def show_qr():
global qr_img
qr_img = ImageTk.PhotoImage(Image.open(qr_img))
qr = tk.Label(frame, image = qr_img)
qr.grid(row =3, columnspan = 3, padx = 5, pady = 5)
qr.config(image = qr_img)
return qr_img
l1 = tk.Label(root, text = "Welcome to QR Code Generator", font=("Calibre", 16), bg="#2c3e50", fg="white")
l1.grid(row =0, columnspan = 2, padx = 5, pady = 5)
frame = tk.Frame(root)
frame.grid()
frame.config(bg="#2c3e50")
l2 = tk.Label(frame, text = "Link you want as a QR Code: ", bg="#2c3e50", fg="white")
l2.grid(row =1, column = 1, padx = 5, pady = 5, sticky="w")
link_name = tk.StringVar(frame, value='hhtps://')
e1 = tk.Entry(frame, textvariable = link_name, width=35)
e1.grid(row =1, column = 2, padx = 5, pady = 5)
b_cre = tk.Button(frame, text = 'Create QR Code', command = lambda: create_QR(link_name))
b_cre.grid(row =2, column = 1, padx = 5, pady = 5)
b_sav = tk.Button(frame, text = 'Show QR Code', command = show_qr)
b_sav.grid(row =2, column = 2, padx = 5, pady = 5)
root.mainloop()
Output:
So, what i wanna do is that when i click the button, a new thing to be added to my project.
I have few tabs, and on the second one (WorkExp) I got Company and job description labels, and i want that whenever i click the button it to add new same labels.
it works, the button, but the thing is add the placement on these new labels is the same as old ones.
I tried while and for cycle but i couldnt make any of them work.
What I have tried:
WorkExp = ttk.Frame(Tabs)
Tabs.add(WorkExp, text = "Work Experience")
######################
def AddExp():
Label(WorkExp, text = "Company/Place", padx = 5, pady = 5).grid(row = 3, column = 1)
Label(WorkExp, text="Job Description", padx=5, pady=5).grid(row = 4 , column=1)
Comp2 = Entry(WorkExp).grid(row=3, column=2)
Work2 = Entry(WorkExp).grid(row=4, column=2)
######################
Label(WorkExp, text = "Company/Place", padx = 5, pady = 5).grid(row = 1, column = 1)
Label(WorkExp, text = "Job Description", padx = 5, pady = 5).grid(row = 2, column = 1)
Comp1 = Entry(WorkExp).grid(row = 1, column = 2)
Work1 = Entry(WorkExp).grid(row = 2, column = 2)
Button(WorkExp, text = "Add Experience", command = AddExp).grid(row = 10, column = 1)
import Tkinter as tk
# Now Start From Here
class App(object):
def new_row(self):
# Create widgets -----
new_entry = tk.Entry(root, width=7)
# Put widgets in grid----------
self.num_rows += 1
new_entry.grid(column=0, row=self.num_rows, sticky='WE')
def __init__(self):
self.num_rows = 1
createRow_button = tk.Button(
root, text='New Row', command=self.new_row)
createRow_button.grid()
root = tk.Tk()
app = App()
root.mainloop()
I'm trying to make a login window. I found a good example code here in Stackoverflow and added it with a simple code using Entry widget. After log in successfully, however, I can't get correct values of textvariable of Entry Widget even though I try to get them with Entry's instance.get().
I've tried many ways I could do. But, I can't find what's wrong.
I'm working with python 2.7. please help.
from Tkinter import *
import tkMessageBox as tm
class LoginFrame:
def __init__(self):
root = Tk()
self.label_1 = Label(root, text="Username")
self.label_2 = Label(root, text="Password")
self.entry_1 = Entry(root)
self.entry_2 = Entry(root, show="*")
self.label_1.grid(row=0, sticky=E)
self.label_2.grid(row=1, sticky=E)
self.entry_1.grid(row=0, column=1)
self.entry_2.grid(row=1, column=1)
self.checkbox = Checkbutton(root, text="Keep me logged in")
self.checkbox.grid(columnspan=2)
self.logbtn = Button(root, text="Login", command =
self._login_btn_clickked)
self.logbtn.grid(columnspan=2)
root.mainloop()
def _login_btn_clickked(self):
username = self.entry_1.get()
password = self.entry_2.get()
#print(username, password)
if username == "1" and password == "1":
#tm.showinfo("Login info", "Welcome John")
app = EntrySample()
app.window.mainloop()
else:
tm.showerror("Login error", "Incorrect username")
class EntrySample:
def __init__(self):
self.window = Tk()
self.window.title("Test Entry")
Label(self.window, text = "Kor").grid(row = 1,
column = 1, sticky = W)
self.kor = IntVar()
self.enKor = Entry(self.window, textvariable = self.kor,
justify = RIGHT).grid(row = 1, column = 2)
Label(self.window, text = "Eng").grid(row = 2,
column = 1, sticky = W)
self.eng = IntVar()
self.enEng = Entry(self.window, textvariable = self.eng,
justify = RIGHT).grid(row = 2, column = 2)
Label(self.window, text = "Math").grid(row = 3,
column = 1, sticky = W)
self.math = IntVar()
self.enMath = Entry(self.window, textvariable = self.math,
justify = RIGHT).grid(row = 3, column = 2)
btComputePayment = Button(self.window, text = "Calculate",
command = self.compute).grid(
row = 4, column = 2, sticky = E)
def compute(self):
total = self.kor.get()+self.eng.get()+self.math.get()
avg = total/3.0
print total
print '%3.2f' %avg
LoginFrame()
I would like a Tkinter button to clear my current Grid, and also go to a function, and I cannot think of how to do it. I have a grid that is a menu, and in another function I have the code for what was just opened by hitting the button.
in short I want a button, when clicked to do this: self.studyGuide and this: self.frame.grid_forget.
Here is my code:
from tkinter import *
class App:
def __init__(self,master):
frame = Frame(master)
frame.grid()
self.sg = Button(frame, text = "Study Guide", command = self.buttonStart, fg="red")
self.sg.grid(row = 2, column = 1)
self.quizlet = Button(frame, text = "Quizlet", command = self.quizlet)
self.quizlet.grid(row = 2, column = 2)
self.flashcard = Button(frame, text = "Flash Cards", command = self.flashcard)
self.flashcard.grid(row = 2, column = 3)
self.quitButton = Button(frame, text = "Quit", command = frame.quit)
self.quitButton.grid(row = 3, column = 2)
self.text = Label(frame, text = "Social Studies Study Tool")
self.text.grid(row = 1, column = 2)
def buttonStart(frame):
self.studyGuide()
self.frame.grid_forget()
def studyGuide(self):
studyGuide = Frame()
studyGuide.pack()
self.sgText = Label(studyGuide, text = "This is not real.")
self.sgText.pack()
def quizlet(self):
print("Quizlet")
def flashcard(self):
print("Flashcards")
root = Tk()
app = App(root)
root.mainloop()
Simply, have the callback passed to the Button constructor call the other 2 functions:
def foo(self):
self.studyGuide()
self.frame.grid_forget()
root = Tk()
my_button = Button(root, text="I'm doing stuff", command=foo)
I am trying to make a tkinter widget that will remember the previous entry. The problem I am having is the that the button method I made erases the previous entry every time its pressed.
from Tkinter import *
class step1():
def __init__(self):
pass
def getTextbook(self):
temp = str(textbook.get())
textbook.delete(0, END)
x = " "
x += temp
print x
def equal_button(self):
print getTextbook(self)
root = Tk()
root.title("step1")
root.geometry("600x600")
s = step1()
app = Frame(root)
app.grid()
label = Label(app, text = "step1")
label.grid()
textbook = Entry(app, justify=RIGHT)
textbook.grid(row=0, column = 0, columnspan = 3, pady = 5)
textbook2 = Entry(app, justify=RIGHT)
textbook2.grid(row=1, column = 0, columnspan = 3, pady = 5)
button1 = Button(app, text = "Return", command = lambda: s.getTextbook())
button1.grid()
button2 = Button(app, text="Equal")
button2.grid()
root.mainloop()
The variable X in your getTextbook() is being overwritten every time you set it to " ". Try a list instead, and append each entry to the list when the button is pressed:
from Tkinter import *
root = Tk()
textbookList = []
def getTextbook():
textbookList.append(textbook.get())
textbook.delete(0,END)
print textbookList
textbook = Entry(root)
textbook.pack()
btn1 = Button(root, text='Return', command=getTextbook)
btn1.pack()
root.mainloop()