I am trying to create a simple GUI in Python to create buttons to access apps within my computer.
I am very new to Python & coding in general so bear with me..
I am getting 0 error messages when running the code but it just isn't displaying the button at the bottom. Here's what I have written out.
Here's a screenshot of my result
import tkinter as tk
from tkinter import filedialog, Text
import os
root = tk.Tk()
canvas = tk.Canvas(root, height=700, width=700, bg="#263D42")
canvas.pack()
frame = tk.Frame(root, bg="white")
frame.place(relwidth=0.8, relheight=0.8, relx=0.1, rely=0.1)
openFile = tk.Button(root, text="Open File", padx=10,
pady=5, fg="white", bg="#263D42")
openFile.pack()
root.mainloop()
Related
I am trying to create a Toplevel window, however, this Toplevel is called from a different file in the same directory within a function.
Apologies I am by no means a tkinter or python guru. Here are the two parts of the code. (snippets)
#File 1 (Main)
import tkinter as tk
from tkinter import *
import comm1
from comm1 import com1
root = tk.Tk()
root.title("")
root.geometry("1900x1314")
#grid Center && 3x6 configuration for correct gui layout
root.grid_rowconfigure(0, weight=1)
root.grid_rowconfigure(11, weight=1)
root.grid_columnconfigure(0, weight=1)
root.grid_columnconfigure(11, weight=1)
#background image
canvas = Canvas(root, width=1900, height=1314)
canvas.place(x=0, y=0, relwidth=1, relheight=1)
bckground = PhotoImage(file='img.png')
canvas.create_image(20 ,20 ,anchor=NW, image=bckground)
#command to create new Toplevel
btn1 = tk.Button(root, text='Top', command=com1, justify='center', font=("Arial", 10))
btn1.config(anchor=CENTER)
btn1.grid(row=4, column=1)
#File 2 (Toplevel)
#command for new window
def com1():
newWindow1 = Toplevel(root)
newWindow1.title("")
newWindow1.geometry("500x500")
entry1 = tk.Entry(root, justify='center' , font=("Arial", 12), fg="Grey")
newWindow1.pack()
newWindow1.mainloop()
The weird part is this worked perfectly for a few minutes and without changing any code it just stopped working.
Where am I going wrong?
You need to pass root as an argument to com1
Also, you only need to start mainloop once, and that should probably be in the main file. You do not need to call it each time you create a new window.
Thanks everyone that answered,
Decided to bypass the problem with better structuring in a single file. :)
I am trying to make a popup for messages such as "Action completed" and I want a button on it to close the popup (A simple OK which quits only the popup). It also sometimes moves behind the window which is annoying. I have some code for the button but it has an issue with the geometry of the shape since the shape isn't exactly defined as it is variable through the size of font and text length.
import tkinter as tk
from tkinter import *
import pygame
import sys
def Text(Text_input, Font, Background, Foreground):
my_window = tk.Tk()
my_window.title(Text_input)
my_window.geometry()
help_label = tk.Label(my_window, font = Font, bg=Background, fg=Foreground, text = Text_input)
help_label.grid(row=0, column=0)
button = tk.Button(my_window, text="QUIT", fg="red", command=quit)
button.pack(side=tk.BOTTOM)
my_window.mainloop()
Text("Hello", "calibri 80", "white", "black")
From my own experience wanting to achieve this, I wrote a simple tkinter code export_success.py as follows:
from tkinter import *
from tkinter import ttk
import sqlite3
from tkinter.ttk import *
root = Tk()
root.geometry('280x100')
root.title("Export Status")
root.config(bg="")
style = ttk.Style()
label_0 = Label(root, text="Export Successful", width=20, background="white", foreground="grey15", font=("Arial, bold", 15)).place(x=50, y=23)
exit1 = Button(root, text='Exit', style='C.TButton', width=11, command=root.destroy).place(x=100, y=60)
root.mainloop()
I then just insert this code os.system('python export_success.py') at the end of my tkinker window that I'm working on.
In this case I'm exporting my information and wanted to know when it is successful.
I am not saying this is best practice and there might be other ways, I just found it to work out for me.
This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 2 years ago.
everyone, I am using Tkinter and I want to open a window after clicking on the button but I have this problem that when I run the program it will open the window not the button and the button is completely useless
this is my code
import tkinter as tk
from tkinter import filedialog, Text
import os
root = tk.Tk()
def addApp():
filename = filedialog.askopenfilename(initialdir="/", title="Select File",
filetypes=(("executable", "*.exe"), ("allfiles", "*.*")))
canvas = tk.Canvas(root, height=700, width=700, bg="#263D42")
canvas.pack()
frame = tk.Frame(root, bg="white")
frame.place(relwidth=0.8, relheight=0.8, relx=0.1, rely=0.1)
openFile = tk.Button(root, text="OpenFile", padx=10,
pady=5, fg="white", bg="#263D42", command=addApp())
openFile.pack()
runApps = tk.Button(root, text="RunApps", padx=10, pady=5, fg="white", bg="#263D42")
runApps.pack()
root.mainloop()
Rename command=addApp() to command=addApp
I can't seem to be able to import allot of the data correctly. For example my script is a simple GUI with a main.py and ipfunc.py the main.py file contains the TKinter GUI code and the ipfunc.py file contains all the functions that each button will perform.
I had this code working perfectly in the older version with all the code put together not using classes and objects for the GUI, however now i'm having allot of trouble trying to import data from file to file.
This is the GUI (main.py) file
from ipfunc import *
from tkinter import *
from tkinter.ttk import *
import tkinter as tk
class gui:
def __init__(self, root):
self.root = root
root.title("IPPing")
root.geometry("380x100+900+300")
self.label = Label(root, text="___________").grid(row=1)
self.label2 = Label(root, text="Enter IP Address").grid(row=2)
e1 = tk.Entry(root)
e1.grid(row=2, column=1)
self.button_addip = Button(text='Add New IP', command=newip).grid(row=0, column=0, pady=2)
self.button_list = Button(root, text='List of IPs', command=contents).grid(row=0, column=1, sticky=tk.SE,
pady=2)
self.button_ping = Button(root, text='Ping all servers', command=scanlist).grid(row=0, column=2, sticky=tk.SE,
pady=2)
self.button_quit = Button(root, text='Quit', command=root.quit).grid(row=0, column=3, pady=2)
root = tk.Tk()
ip_gui = gui(root)
root.mainloop()
This is the functions that i want to execute when buttons are pressed, but theirs errors when using the functions that i cant solve it, please help.
https://gist.github.com/Hontiris1/83c8229313d971214d8610c21fe2e676
I am making a small program with the tkinter plugin in python. I want to make a page scrolling as you can do in the browser. Is it possible? and how do you do it?
window2 = Tk()
window2.title("RPG SM Beta 0.1")
window2.geometry('150x200')
window2.configure(background="#5C5858")
window2.iconbitmap("Logo.ico")
window2.resizable(False, False)
write1 = Text(window2, height=5, width=17, bg="white")
write1.place(x=6, y=30)
lab2 = Label(window2, text='Lorem ipsum', bg="#5C5858", fg='white', font='none 12 bold')
lab2.place(x=4, y=5)
The most command solution is to use a Canvas widget as the container. Anything added to a canvas with the create_window method will be scrollable.