Does anyone know why it will not show the taskbar in my code. I am trying to get the top to say Exit and information in the file drop down menu. I am kind of new to tkinter and i just need a bit of help please. Also if you have any suggestions on how to improve it, i would really appreciate it!
My code is below:
from time import sleep
from tkinter import *
from tkinter import messagebox, ttk, Tk
root = Tk()
class GUI():
def taskbar(self):
menu = Menu(root)
file = Menu(menu)
file.add_command(label="Exit", command=self.exit_GUI)
file.add_command(label = "Information", command=self.info_popup)
def Main_Menu(self):
topFrame = Frame(root)
topFrame.pack()
bottomFrame = Frame(root)
bottomFrame.pack(side=BOTTOM)
Income_button = Button(topFrame, text="Enter your incomes", command=self.Income)
Expense_button = Button(topFrame, text="Enter your expenses", command=self.Expense)
Total_button = Button(bottomFrame, text="View Results", command=self.Total)
Income_button.pack()
Expense_button.pack()
Total_button.pack()
def Income(self):
pass
def Expense(self):
pass
def Total(self):
pass
def exit_GUI(self):
exit()
def info_popup(self):
pass
g = GUI()
g.taskbar()
g.Main_Menu()
g.Income()
g.Expense()
g.Total()
g.info_popup()
root.mainloop()
You need to change the taskbar function to:
def taskbar(self):
menu = Menu(root)
file = Menu(menu)
file.add_command(label="Exit", command=self.exit_GUI)
file.add_command(label = "Information", command=self.info_popup)
root.config(menu=file)
This will tell the window to use the menubar.
Related
I was trying to make a application by tkinter, and I wanted to make a checkbutton in menubar. However, I don't know how to make the checkbutton already checked when I run the code.
Here's my code
import tkinter as tk
def func():
#some code here
var = tk.BooleanVar
win = tk.Tk()
menubar = tk.Menu(win)
optmenu = tk.Menu(menubar, tearoff=0)
menubar.add_cascade(label='Options', menu=optmenu)
optmenu.add_checkbutton(label='xyz', variable=var, onvalue=1, offvalue=0, command=func)
win.config(menu=menubar)
win.mainloop()
use this code below:
import tkinter as tk
def func():
pass
#some code here
win = tk.Tk()
var = tk.StringVar(win,'on')
menubar = tk.Menu(win)
optmenu = tk.Menu(menubar, tearoff=0)
menubar.add_cascade(label='Options', menu=optmenu)
optmenu.add_checkbutton(label='xyz', variable=var, onvalue='on', offvalue='off', command=func)
win.config(menu=menubar)
win.mainloop()
have fun :)
if i click noticeall in menubar, i wanna it to be displayed on textarea at mainframe.py
how can i write code in def noticeall.. plz
main.py file code is
import mainframe
mainframe.show()
mainframe.py file code is
def show ():
frame = tkinter.Tk()
frame.title('test')
frame.geometry('800x600')
frame.resizable(False, False)
sf = ScrolledFrame(frame, width=800, height=600)
sf.pack(side="top", expand=1, fill="both")
sf.bind_arrow_keys(frame)
sf.bind_scroll_wheel(frame)
menu_bar = menubar.make_menubar(frame,)
frame.config(menu=menu_bar)
inner_frame = sf.display_widget(Frame)
entry = Entry(inner_frame, width=750)
entry.pack()
entry.focus_set()
textarea = Text(inner_frame, width=750, height=200)
textarea.pack()
frame.mainloop()
menubar.py file code is
def make_menubar(frame):
menubar = Menu(frame)
file_menu = Menu(menubar, tearoff=0)
file_menu.add_command(label="noticeall",command=noticeall)
def noticeall():
notice_list = ncontroller.select_all()
print_list(notice_list)'
plz recommend next line code
print_list.insert(textarea)??? - i cant, textarea is local variable in mainframe.py
def make_menubar(frame, textarea):
menubar = Menu(frame)
file_menu = Menu(menubar, tearoff=0)
file_menu.add_command(label="noticeall", command=lambda: noticeall(textarea))
return menubar
def noticeall(textarea):
notice_list = ncontroller.select_all()
textarea.insert(notice_list)
In fact, the best way is to create a class. Pass parameters through self.
I need help, I am doing a budget calculator and using tkinter for the first time and wondered why it is not working...
When I run it, it will just end straight away and when I put the root = Tk() at the end it comes up with an error.
I really need help, my code is below...
from time import sleep
from tkinter import *
from tkinter import messagebox, ttk, Tk
root = Tk()
class GUI():
def taskbar(self):
menu = Menu()
file = Menu(menu)
file.add_command(label="Exit", command=self.exit_GUI)
file.add_command(label = "Information", command=self.info_popup)
def Main_Menu(self):
topFrame = Frame(root)
topFrame.pack()
bottomFrame = Frame(root)
bottomFrame.pack(side=BOTTOM)
Income_button = Button(topFrame, text="Enter your incomes", command=self.Income)
Expense_button = Button(topFrame, text="Enter your expenses", command=self.Expense)
Total_button = Button(bottomFrame, text="View Results", command=self.Total)
Income_button.pack()
Expense_button.pack()
Total_button.pack()
def Income(self):
pass
def Expense(self):
pass
def Total(self):
pass
def exit_GUI(self):
exit()
def info_popup():
pass
g = GUI()
g.Main_Menu()
g.taskbar()
g.Income()
g.Expense()
g.Total()
g.exit_GUI()
g.info_popup()
root.mainloop()
You are exiting before you ever get to the mainloop with:
g.exit_GUI()
That method is calling the standard exit() and stopping the entire script. Remove or comment out the above call. You will also need to add self as an argument to info_popup to get your script to run:
def info_popup(self):
pass
So, what I am trying to do is open a file when pressing a button and displaying the contents in a listbox. This is what I have so far, but I am not getting the listbox to display, let alone get the info to be in the listbox:
#!/usr/bin/perl -w
import time
from Tkinter import *
import tkFileDialog
def listbox(listbox):
def open_file():
file = tkFileDialog.askopenfilename()
openFile = open(file)
for line in openFile:
listbox.insert(END, line)
open_file()
class App:
def __init__(self, parent):
frame = Frame(parent.title("Buttons"))
frame.pack()
root.pack_propagate(0)
self.exit = Button(frame, text="QUIT", fg="red", command=frame.quit)
self.exit.pack(side=LEFT)
self.open = Button(frame, text="Open...", command=self.call_listbox)
self.open.pack(side=LEFT)
frame.listbox = Frame()
scrollme = Scrollbar(frame.listbox)
self.listbox = Listbox(frame.listbox, yscrollcommand = scrollme.set)
scrollme.config(command = self.listbox.yview)
scrollme.pack(side = RIGHT, fill = Y)
self.listbox.pack()
self.listbox.insert(END, "Code:")
def call_listbox(self):
listbox(self.listbox)
root = Tk()
app = App(root)
root.mainloop()
any suggestions? thanks
You are forgetting to pack the frame that contains the listbox.
FWIW, your overloading of the name "listbox" makes your code very confusing - you have def listbox(listbox), self.listbox and frame.listbox. And you also have call_listbox and the Listbox class to add to the confusion.
Is there a way to return something when a button is pressed?
Here is my sample program. a simple file reader. Is the global variable to hold text contents the way to go since I can't return the contents?
from Tkinter import *
import tkFileDialog
textcontents = ''
def onopen():
filename = tkFileDialog.askopenfilename()
read(filename)
def onclose():
root.destroy()
def read(file):
global textcontents
f = open(file, 'r')
textcontents = f.readlines()
text.insert(END, textcontents)
root = Tk()
root.title('Text Reader')
frame = Frame(root)
frame.pack()
text = Text(frame, width=40, height=20)
text.pack()
text.insert(END, textcontents)
menu = Menu(root)
root.config(menu=menu)
filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="Open...", command=onopen)
filemenu.add_command(label="Exit", command=onclose)
mainloop()
Tk(inter) is event-based, which means, that you do not return values, but bind callbacks (functions) to actions.
more info here: http://effbot.org/tkinterbook/button.htm
If you meant signal back to the user, here's some sample code:
import Tkinter
import tkMessageBox
top = Tkinter.Tk()
def helloCallBack():
tkMessageBox.showinfo( "Hello Python", "Hello World")
B = Tkinter.Button(top, text ="Hello", command = helloCallBack)
B.pack()
top.mainloop()
and the source: Python - Tkinter Button tutorial