I am eriting code in pycharm with tkinter but the window is not opening. May someone assist?
`
import tkinter
window = tkinter.Tk()
button = tkinter.Button(window, text="Do not press this button! >:-(", width=40)
button.pack(padx=10, pady=10)
`
i tried checking my script for bugs but nothing
This has nothing to do with Pycharm, but with tkinter library and how to use it.
You are missing 2 important stuff:
Button is in ttk.py file inside tkinter library: from tkinter import ttk
Execute the whole script with mainloop
Try this:
import tkinter
from tkinter import ttk # Import ttk file from tkinter library
window = tkinter.Tk()
window.title("Coolest title ever written")
button = ttk.Button(window, text="Do not press this button! >:-(", width=40) # Use Button from the import ttk file
button.pack(padx=10, pady=10)
window.mainloop() # Execute the whole script
Related
I have used a tkinter.filedialog with the pyglet library. But an old file explorer pops up when I open that filedialog. Normally it opens the native/modern file explorer of windows, but it is showing this behavior after importing the pyglet module. Any solution to get back the new filedialog?
Here is a basic code to reproduce the issue:
from tkinter import Tk, Button, filedialog
import pyglet #Try commenting this import to test the filedialog
root= Tk()
root.geometry("200x100")
window = pyglet.window.Window(20, 20, "Pyglet_window") #Pyglet window
def open_file():
file=filedialog.askopenfilename()
btn= Button(root, width=10,bg="#FFFFFF",fg="black", text="Open File",command=open_file)
btn.grid(padx=50,pady=20)
root.mainloop()
My results:
When i press the start button, it runs the other script but only shows it in the other terminal, not in the GUI scrolledtext box i have. is there a simple fix for my problem? not finding an answer anywhere.
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
import sys
import os
import tkinter.scrolledtext as tkst
root = Tk()
root.title("WLP")
root.geometry("950x450")
root.iconbitmap("Vo1d.ico")
root.configure(bg='#0c0c0c')
def clickstart():
os.system('python WLP.py')
Button1 = Button(root, text="START", bg="#0c0c0c", fg="#C0C0C0", command=clickstart) #)
Textbox = tkst.ScrolledText(root, width=75, height=10, bg="#0c0c0c", fg="#C0C0C0")
Button1.grid(row=10, column=5, pady=15)
Textbox.grid(row=17, column=5)
root.mainloop()
This is far from a good way to do it, but this should work (after importing subprocess)
def clickstart():
output = subprocess.run(['python', 'WPL.py'], stdout=subprocess.PIPE, check=True)
output = str(output.stdout, encoding='utf-8')
Textbox.insert(1.0, output)
I want to detect the right click event on tkinter Menu command.
Consider code below.
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
menu_button = ttk.Menubutton(root, text="MENU")
menu_button.grid()
m = tk.Menu(menu_button, tearoff=False, activeborderwidth=0)
menu_button["menu"] = m # To avoid garbage collection
m.add_command(label="an option", command=lambda: print("option1"))
m.add_command(label="another option", command=lambda: print("option2"))
root.mainloop()
When I click an option or another option, the commands are called as expected. But want I want to do is catch right click event. Can anyone knows that how can I detect it?
use button.bind("<Button-3>", event). Consider this code:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
button = tk.Button(root, text='right click this')
button.pack()
button.bind("<Button-3>", lambda e: print('You right clicked'))
root.mainloop()
so i want to open a new python tkinter program when i click a button in the GUI. As soon as i run the code ProgramB runs. When i close this GUI the main GUI runs but when i click the button in this GUI, it doesn't open ProgramB. I'm Learning pyhton and tkinter and don't know how to fix this problem. Thank you in advance.
My main code:
from tkinter import *
from tkinter.ttk import *
import basis_tkinter
import subprocess
mainwindow = Tk()
mainwindow.title= "test opening new file"
def open():
subprocess.call("basis_tkinter.py", shell=True)
btnopen = Button(mainwindow, text="open new program", command=lambda: open).pack()
mainwindow.mainloop()
Program B (basis_tkinter):
from tkinter import *
MainWindow = Tk()
MainWindow.title("Title")
MainWindow.iconbitmap(r'C:\Users\Gebruiker\Documents\school 2020-2021\stage\interface\Afbeeldingen\Apex icon.ico')
Background= Label(MainWindow, image=PhotoImage(file=r"C:\Users\Gebruiker\Documents\school 2020-2021\stage\interface\Afbeeldingen\Apex logo.png"))
Background.place(x=0,y=0, relwidth=1, relheight=1)
MainWindow.geometry("400x400")
def clicked():
label = Label(MainWindow, text="You clicked").pack()
button = Button(MainWindow, text="Click me", command=clicked).pack()
MainWindow.mainloop()
I've read some post on stack overflow,Issues intercepting subprocess output in real time, Redirect command line results to a tkinter GUI, i know i have to use threading and queue in tkinter, but I am still can't do the same thing because I am a beginner in program,please help.
The goal: When press a button, getting the 'top' command output and realtime display in tkinter text widget
The issue: I've tried to follow the code, but still cannot get the output, but I have not idea how to make it work.
from tkinter import *
import tkinter as tk
import subprocess
from threading import Thread
from queue import Queue
window = tk.Tk()
window.title('realtime')
window.geometry('800x400')
text = tk.Text(window)
text.pack()
button = tk.Button(window, text= 'Press')
button.pack()
window.mainloop()
This is only the gui outlook, please help
top refreshes itself now and then and I'm guessing that's the behavior you want to capture with threading and whatnot. However in this case it would be much easier to ask top to only run once, and have tkinter do the timing and refreshing:
import tkinter as tk
from sh import top
def update_text():
text.delete(0.0, tk.END)
text.insert(0.0, top('-b', n=1))
window.after(1000, update_text) # call this function again in 1 second
window = tk.Tk()
window.title('realtime')
window.geometry('800x400')
text = tk.Text(window)
text.pack()
button = tk.Button(window, text= 'Press', command=update_text)
button.pack()
window.mainloop()
You may need to install sh to run top like I did, or use subprocess.check_output if you want.
text.insert(0.0, subprocess.check_output(['top', '-b', '-n 1']))