import tkinter
from tkinter import ttk
from tkinter import *
window = tkinter.Tk()
window.title("wasans")
window.geometry('640x400')
window.resizable(False, False)
textbook = ['국어','영어','social']
textbookselecter = ttk.Combobox(window, text="교과서 선택", values=textbook)
textbookselecter.grid(column=0, row=0)
textbookselecter.place(x=140, y=200)
textbookselecter.set("목록 선택")
textbooksuntack = ttk.Label(window, width=11, borderwidth=2, background="#8182b8")
textbooksuntack.grid(column=0, row=0)
textbooksuntack.place(x=170, y=170)
textbooksuntack.anchor = CENTER
naeyong = textbookselecter.get()
if naeyong == "social":
open("../social.py", "w")
window.mainloop()
How can I open a py file using if statement and open?
Also, please give your overall review of the code.
I am not good at English, so there may be awkward sentences using a translator. Please understand.
You basically need to check every time the ttk.Combobox is changed to check if 'social' is generated.
ttk.Combobox raises a "<<ComboboxSelected>>" virtual event when selected.
import tkinter
from tkinter import ttk
from tkinter import *
window = tkinter.Tk()
window.title("wasans")
window.geometry('640x400')
window.resizable(False, False)
def callback(eventObject):
if textbookselecter.get() == "social":
print("Opening File...")
#file=open("...../social.py","r+")
textbook = ['국어','영어','social']
textbookselecter = ttk.Combobox(window, text="교과서 선택", values=textbook)
textbookselecter.grid(column=0, row=0)
textbookselecter.place(x=140, y=200)
textbookselecter.set("목록 선택")
textbooksuntack = ttk.Label(window, width=11, borderwidth=2, background="#8182b8")
textbooksuntack.grid(column=0, row=0)
textbooksuntack.place(x=170, y=170)
textbooksuntack.anchor = CENTER
textbookselecter.bind("<<ComboboxSelected>>",callback)
window.mainloop()
Related
I have a bokeh code that I open it with the terminal typing:
bokeh serve --show graph2d.py
how can I make a tkinter open this bokeh.py(graph2d.py)?
Please help with this, I dont find how to make it work.
this is the tkinter:
import tkinter as tk
from tkinter import *
import graph2d
"""import tkinter as Tk
from tkinter import ttk"""
window = Tk()
window.title("Welcome to PokerScatter app")
"""
WINDOW_SIZE = "600x400"
root = tk.Tk()
root.geometry(WINDOW_SIZE)
"""
selected = IntVar()
rad1 = Radiobutton(window,text='2d Scatter', value=1, variable=selected, command=graph2d)
rad2 = Radiobutton(window,text='3d Scatter', value=2, variable=selected)
rad3 = Radiobutton(window,text='Pie Chart', value=3, variable=selected)
rad4 = Radiobutton(window,text='Histogram', value=4, variable=selected)
def clicked():
print("Processing")
btn = Button(window, text="Show", command=clicked)
rad1.grid(column=0, row=0)
rad2.grid(column=2, row=0)
rad3.grid(column=0, row=1)
rad4.grid(column=2, row=1)
btn.grid(column=6, row=0)
window.mainloop()
I'm trying to run this code but it's giving "_tkinter.TclError: bad window path name ".!button2" " this error.
But If I run the same code without ebook function, it's working fine. As I'm new to coding, any help will be appreciated.
from tkinter import *
root = Tk()
root.title("Application")
root.resizable(width=False, height=False)
mylabel0= Label(root, text=" ")
mylabel0.grid(row=0, column=0)
def ebook():
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
from gtts import gTTS
import pdfplumber
book = tk.Tk()
book.minsize(width=150, height=200)
book.maxsize(width=300, height=420)
canvas1 = tk.Canvas(book, width=300, height=420, bg='azure3', relief='raised')
canvas1.grid()
label1 = tk.Label(book, text="PDF Audio Store", bg='azure3')
label1.config(font=('helvetica', 16))
canvas1.create_window(150, 20, window=label1)
final = ""
def get_pdf():
global final
global pdf_checker
try:
import_file_path = filedialog.askopenfilename()
if (str(import_file_path).endswith(".pdf")):
pdf = pdfplumber.open(import_file_path)
pdf_checker = pdf
n = len(pdf.pages)
for page in range(n):
data = pdf.pages[page].extract_text()
final = final + "\n" + data
messagebox.showinfo("Information", "PDF is imported")
else:
raise Exception("Provide .pdf file only")
except Exception as e:
messagebox.showerror("Error", "{}".format(e))
else:
print("Continue to conversion")
brows_pdf = tk.Button(text="PDF entry", command=get_pdf, bg='royalblue', fg="white", font=('helvetica', 12, 'bold'),
border=0,
activebackground="green")
It's giving error in this(👇) area specifically, create_window.Error Image
canvas1.create_window(150, 60, window=brows_pdf)
def convert_audio():
global final
global pdf_checker
try:
print("File Information")
print(pdf_checker)
export_file_path = filedialog.asksaveasfilename(defaultextension=".mp3")
final_file = gTTS(text=final, lang="en")
final_file.save(export_file_path)
messagebox.showinfo("Information", "Audio file generated")
except NameError:
messagebox.showwarning("Warning", "Import PDF first")
audio_button = tk.Button(text="Convert to Audio", command=convert_audio, bg='royalblue', fg="white",
font=('helvetica', 12, 'bold'),
border=0,
activebackground="green")
canvas1.create_window(150, 100, window=audio_button)
book.mainloop()
button_ebook= Button(root, text="E-Book📖", font=30, padx=25, pady=25, command=ebook, fg="black", bg="white", borderwidth=1)
button_ebook.grid(row=3, column=2, padx=10)
root.mainloop()
The root of the problem is that you can't use a button in one window that is a descendant of another window.
The first problem is that you are creating two instances of Tk. Buttons created in one instance are not available in the other. In this case, your button is in the original instance of Tk but your canvas is in the other.
The second problem is similar. Even when replacing the second instance of Tk, the button is in the root window and thus can't be used as a child of the canvas which is in the second window.
The solution is to use Toplevel for the second window, and to make sure the button is a descendant of the second window and/or the canvas.
I've started learning Tkinter on Python few weeks ago and wanted to create a Guess Game. But unfortunately I ran into a problem, with this code the text for the rules ( in the function Rules; text='Here are the rules... ) doesn't appear on the window ( rule_window).
window = Tk()
window.title("Guessing Game")
welcome = Label(window,text="Welcome To The Guessing Game!",background="black",foreground="white")
welcome.grid(row=0,column=0,columnspan=3)
def Rules():
rule_window = Tk()
rule_window = rule_window.title("The Rules")
the_rules = Label(rule_window, text='Here are the rules...', foreground="black")
the_rules.grid(row=0,column=0,columnspan=3)
rule_window.mainloop()
rules = Button(window,text="Rules",command=Rules)
rules.grid(row=1,column=0,columnspan=1)
window.mainloop()
Does anyone know how to solve this problem?
In your code you reset whatever rule_window is to a string (in this line: rule_window = rule_window.title(...))
Try this:
from import tkinter *
window = Tk()
window.title("Guessing Game")
welcome = Label(window, text="Welcome To The Guessing Game!", background="black", foreground="white")
welcome.grid(row=0, column=0, columnspan=3)
def Rules():
rule_window = Toplevel(window)
rule_window.title("The Rules")
the_rules = Label(rule_window, text="Here are the rules...", foreground="black")
the_rules.grid(row=0, column=0, columnspan=3)
rules = Button(window, text="Rules", command=Rules)
rules.grid(row=1, column=0, columnspan=1)
window.mainloop()
When you want to have 2 windows that are responsive at the same time you can use tkinter.Toplevel().
In your code, you have initialized the new instances of the Tkinter frame so, instead of you can create a top-level Window. What TopLevel Window does, generally creates a popup window kind of thing in the application. You can also trigger the Tkinter button to open the new window.
from tkinter import *
from tkinter import ttk
#Create an instance of tkinter window
root= Tk()
root.geometry("600x450")
#Define a function
def open_new():
#Create a TopLevel window
new_win= Toplevel(root)
new_win.title("My New Window")
#Set the geometry
new_win.geometry("600x250")
Label(new_win, text="Hello There!",font=('Georgia 15 bold')).pack(pady=30)
#Create a Button in main Window
btn= ttk.Button(root, text="New Window",command=open_new)
btn.pack()
root.mainloop()
So till now I want to make a simple button but it gives me an error screen, what am I doing wrong? Here's my code:
import tkinter as tk
import math
import time
tk = tk.Tk()
tk.geometry()
tk.attributes("-fullscreen", True)
exit_button = tk.Button(tk, text = "Exit", height = 2, width = 2, command = tk.destroy)
exit_button.place(x=1506, y=0)
tk.mainloop()
You are shadowing tk with something else:
import tkinter as tk
root = tk.Tk()
root.geometry()
root.attributes("-fullscreen", True)
exit_button = tk.Button(root, text="Exit", height=2, width=2, command=root.destroy)
exit_button.place(x=1506, y=0)
tk.mainloop()
You cannot use tk = tk.Tk(), because you are also referring to tkinter as tk. So either:
Change your imports(not recommended):
import tkinter as _tk
tk = _tk.Tk() # And so on..
or change your variable name(recommended):
root = tk.Tk() # And change tk.geometry to root.geometry() and so on
I made a form in Tkinter but when it is anwered I need the information to concatenate to a textbox.
Let's say I select I live in Chicago, how do I send that information to the scrolled text when I press the button "send"?
lblCd= Label(ventana,text="Location:",font= ("Arial", 20),
fg="blue", bg="white").place(x=70,y=400)
combo['values']=("Chicago","NY", "Texas")
combo.current(1)
combo.place(x=260, y=400)
Btn=Button(ventana, text="Send",)
Btn.place(x=200,y=500)
txt=scrolledtext.ScrolledText(ventana, width=40, height=10)
txt.place(x=260, y= 550)
txt.insert(INSERT, Nombre)
You can use Button(..., command=function_name) to execute function_name() when you press button. This function can get selected value and insert in scrolledtext.
import tkinter as tk
import tkinter.ttk as ttk
import tkinter.scrolledtext as scrolledtext
def send_data():
txt.insert('end', combo.get() + '\n')
root = tk.Tk()
label = tk.Label(root, text="Location:")
label.pack()
combo = ttk.Combobox(root, values=("Chicago","NY", "Texas"))
combo.pack()
combo.current(0)
button = tk.Button(root, text="Send", command=send_data)
button.pack()
txt = scrolledtext.ScrolledText(root)
txt.pack()
root.mainloop()