I'm really stuck on a basilary things: I have this code
from tkinter import *
import sys
import subprocess
import tkinter as tk
def cd():
f=(subprocess.check_output("net view"))
e=(f.decode(sys.stdout.encoding))
label1=Label(text=e).pack()
def mainscreen():
mainscreen=Tk()
mainscreen.title("Terfysgol's kit V 2.0")
frame1=Frame(mainscreen)
frame1.pack()
puls1=Button(frame1,text="List of device", borderwidth= "2",command= cd).pack()
mainscreen()
When I run it all the time that I press the button it create a new label but I only want to update the text of the label1.
This is what you are after:
def cd():
f=(subprocess.check_output("net view"))
e=(f.decode(sys.stdout.encoding))
label1.config(text = e)
and then at the top of your program after your imports you need to put:
label1 = Label()
label1.pack()
Please note that I'm not suggesting this is good program structure, but that is up to you to sort out. This answer is just a quick fix to provide you with enough information to work out the rest of what you need.
Also you can remove the import tkinter as tk line already imports tkinter.
Related
I have created a simple tKinter Gui with PAGE builder and I am able to click a button and execute the corresponding command function within it. But when I try to get a value of a specific text box within the function I get various errors mostly no such property found. I have tried adding self and the class name into the property and even passing the property from the class as well as making it a function within that class but I still can't seem to access the values of the textbox 'Username'. I would really appreciate any help on how to get those text box values within the function as I have been researching for hours but still cannot make it work. Also if anyone knows of any good tutorial on this topic would help tremendously. Thank you.
The project has 2 files: (I've tried to remove the non essential code)
MacUpdaterPageDesign.py
import sys
import tkinter as tk
import tkinter.ttk as ttk
from tkinter.constants import *
import os.path
_script = sys.argv[0]
_location = os.path.dirname(_script)
import MacUpdaterPageDesign_support
class Toplevel1:
def __init__(self, top=None):
top.title("Mac Updater")
top.configure(background="#d9d9d9")
self.top = top
self.MainFrame = tk.Frame(self.top)
self.MainFrame.place(relx=0.0, rely=0.18, relheight=0.811
, relwidth=1.099)
self.Username = tk.Text(self.MainFrame)
self.Username.place(relx=0.15, rely=0.081, relheight=0.048
, relwidth=0.279)
#this button calls the CopyMACfunc on the support page
self.CopyMAC = tk.Button(self.MainFrame)
self.CopyMAC.place(relx=0.143, rely=0.846, height=34, width=117)
self.CopyMAC.configure(command=MacUpdaterPageDesign_support.CopyMACfunc)
self.CopyMAC.configure(text='Copy MAC')
MacUpdaterPageDesign_support.py
import sys
import tkinter as tk
import tkinter.ttk as ttk
from tkinter.constants import *
import MacUpdaterPageDesign
def main(*args):
'''Main entry point for the application.'''
global root
root = tk.Tk()
root.protocol( 'WM_DELETE_WINDOW' , root.destroy)
# Creates a toplevel widget.
global _top1, _w1
_top1 = root
_w1 = MacUpdaterPageDesign.Toplevel1(_top1)
root.mainloop()
def CopyMACfunc(*args):
#this part must retrieve the value in from Username
#tried many variations of below but throws error
username = MacUpdaterPageDesign.Username.get("1.0",END)
print(username)
if __name__ == '__main__':
MacUpdaterPageDesign.start_up()
Actually while writing this question I finally got it to work:
username = _w1.Username.get("1.0",END)
it did work with the following but not sure if this would be the right way to do it per say. Maybe their are better ways if anyone knows. Also would appreciate any recommendation for a good tutorial or where to learn all of this type of info. Thanks
I am trying a "Hello World" test on a GUI window, imported from tkinter. The problem starts when I try to run it and an error pops up in the terminal of VScode:
cannot import name 'geometry' from 'tkinter'.
import csv
from fileinput import filename
import os
from tkinter import *
from tkinter import ttk
from tkinter import Tk, Button, Frame, Entry, END
from tkinter import geometry
class attendance_tester(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
root = Tk()
root.geometry("800x800")
frm = ttk.Frame(root, padding=10)
frm.grid()
ttk.Label(frm, text="Hello World!").grid(column=0, row=0)
ttk.Button(frm, text="Quit", command=root.destroy).grid(column=1, row=0)
app = attendance_tester(master=root)
app.master.title("Student Attendance Program")
app.mainloop()
root.destroy()
from attendance_create import (func_create, func_edit_add,
func_ask_edit, func_edit_change, func_edit_sub,
func_edit_view, func_open, func_first_change)
with open(r"C:\\Users\\user_name\\Desktop\\attendance_sheet.csv") as file:
csv_file_read = csv.reader(file)
for row in csv_file_read:
print(row)
func_open()
attend_test = attendance_tester(root)
root.mainloop()
I have tried seemingly every solution I could find on the internet. I really need some help.
You don't need to import geometry to use it. Just remove the import line and it should work.
you shouldnt import it from the first place you just need to import the tkinter itself just remove that line and it should be just fine
There is no reason to import geometry, it works without that import line.
Also, you have a few duplicate imports which may become confusing. It's better to stick to fewer imports:
from tkinter import *
from tkinter import ttk
from tkinter import Tk, Button, Frame, Entry, END
In the following code I get an error when leaving out "from tkinter import *. I am confused as to the reason as I import tkinter as tk.
I tried modifying the function set_text without success.
The error is on
self.e.delete(0,END)
NameError: name 'END' is not defined`
Code:
from tkinter import *
import tkinter as tk
from tkinter import ttk
class HelperFun():
def set_text(self,parent,text):
self.e.delete(0,END)
self.e.insert(0,text)
return
def __init__(self,parent):
self.parent=parent
self.e = tk.Entry(self.parent,width=10)
self.e.pack()
self.b1 = tk.Button(self.parent,text="animal",
command=lambda:self.set_text(self.parent,"animal"))
...
root=tk.Tk()
HelperFun(root)
root.mainloop()
Thank you.
When you use, from Tkinter import *, it imports all of the constants from that package, like END.
To get away without using this, you'd need to use the dot operator, like tk.END, otherwise it won't be defined.
If you import tkinter as tk then you need to refer to END as tk.END.
Importing from tkinter import * imports everything in tkinter, making it so you don't have to use the tk qualifying prefix.
replace the line
self.e.delete(0,END)
with
self.e.delete(0,"end")
Your code isn't doing anything. And it is not showing Button
You cannot use self.e simultaneously in the set text() function.
The Button command isn't right.
I modified the code to make it more readability:
import tkinter as tk
class HelperFun():
def __init__(self, parent):
self.parent=parent
self.e = tk.Entry(self.parent,width=10)
self.e.pack()
self.b1 = tk.Button(self.parent,text="animal",
command=lambda:self.set_text(self.e.get()))
self.b1.pack()
self.lb = tk.Label(self.parent, width=10)
self.lb.pack()
def set_text(self, _text):
print(_text)
self.lb.configure(text=_text)
return
root=tk.Tk()
app = HelperFun(root)
root.mainloop()
Screenshot before and after clicking button:
I think in "(0,END)" you need to use 1.0 instead of 0.
Try "(1.0,END)"
END is like Button and Label ...
u have to write tk.END if u (import tkinter as tk)
I'm trying to include Autocomplete in my combobox but it's not working. Received an error when I'm trying to use tkentrycomplete. Below is the code I'm using for combobox, please point out my mistakes and help me out. Thank you
from tkinter import *
from tkinter.ttk import *
from tkinter import ttk
import tkinter as tk
import pandas as pd
comboExample1 = ttk.Combobox(window, width=30, values=list(df3["MFG Device"].unique()))
# comboExample1.current(0)
val = tk.StringVar()
comboExample1 = tkentrycomplete.AutocompleteCombobox(textvariable=val)
comboExample1.place(x=90, y=70)
comboExample1.bind("<<ComboboxSelected>>", select_device)
Error:
comboExample1 = tkentrycomplete.AutocompleteCombobox(textvariable=val)
NameError: name 'tkentrycomplete' is not define
tkentrycomplete is not a thing, I think what you need to do is comboExample1.AutocompleteCombobox(textvariable=val)
(I put this as an answer since I don't have enough rep to comment)
Edit:
try this:
comboExample1['values'] = val
(you may need to put this into a function and then bind it)
I am using a calendar widget for Python. And I need to call the widget when a button is clicked.
The situation is that I cannot find what is the method in the calendar class that displays the widget itself.
The calendar class was taken from here:
http://www.eurion.net/python-snippets/snippet/Calendar_Date%20picker.html
Here are my imports:
from tkinter import *
from tkinter import ttk
import tkinter.messagebox
import time
import requests #needs to be installed
import pymysql #needs to be installed
import csv
import win32com.client #needs to be installed
from calendar import Calendar
import datetime
Here is the button creation:
# Calendar Buttons
calBut=ttk.Button(f2, width=4, text="Cal", command=Calendar.what_method?).grid(column=3,row=1, sticky=W)
As far as I know, I can just set the command of the button to call the widget display method located in the calendar class.
How to get the method that displays the calendar widget each time my button is clicked? None of the ones showing are displaying the widget.
Using Python 3.3.5
Spider
WinPython 3.3.5
**EDIT**
The program has tabs and the f2 indicates the tab where the button will be.
from tkinter import *
from tkinter import ttk
import tkinter.messagebox
import time
import requests #needs to be installed
import pymysql #needs to be installed
import csv
import win32com.client #needs to be installed
import datetime
from calendar import Calendar
import calendar
#################################
# Create Button Click Calendar
def callback():
root2=Toplevel(f2)
ttkcal = Calendar(root2,firstweekday=calendar.SUNDAY)
ttkcal.pack(expand=1, fill='both')
root2.update()
root2.minsize(root2.winfo_reqwidth(), root2.winfo_reqheight())
# Calendar Buttons
b=ttk.Button(f2, width=4, text="Cal", command=callback).grid(column=3,row=1, sticky=W)
When I press the button, it opens the calendar window, but it is empty. And the console gives me error:
TypeError: __init__() got multiple values for argument 'firstweekday
Thank you
Not so easy. The problem is that you mix the two GUI libraries. Therefore it is necessary two main event loops (at least): one for Tkinter code and one for PyQt code.
One way to do what you want - using subprocess and threading modules to run calendar.py in different thread. Example:
from tkinter import *
from tkinter import ttk
import subprocess
import threading
master = Tk()
def callback():
subprocess.call('python calendar.py')
b=ttk.Button(master, width=4, text="Cal", command=lambda:threading.Thread(target=callback).start()).grid(column=3,row=1, sticky=W)
mainloop()
Another way - creating Qt main event loop inside callback function (dirty solution):
from tkinter import *
from tkinter import ttk
from calendar import Calendar
import sys
from PyQt4 import QtGui
master = Tk()
def callback():
app = QtGui.QApplication(sys.argv)
gui = Calendar()
gui.show()
app.exec_()
b=ttk.Button(master, width=4, text="Cal", command=callback).grid(column=3,row=1, sticky=W)
mainloop()
EDIT: How to call widget.
First of all, look at this answer, and modify your ttkcalendar.py as kalgasnik suggested. Then try this:
from tkinter import *
from tkinter import ttk
from ttkcalendar import Calendar
import calendar
master = Tk()
def callback():
root2=Toplevel(master)
ttkcal = Calendar(root2,firstweekday=calendar.SUNDAY)
ttkcal.pack(expand=1, fill='both')
root2.update()
root2.minsize(root2.winfo_reqwidth(), root2.winfo_reqheight())
b=ttk.Button(master, width=4, text="Cal", command=callback).grid(column=3,row=1, sticky=W)
mainloop()
EDIT 2. Solving the problems
Ok, it seems I found all problems.
Actually, you import twice the same module - standard calendar module:
from calendar import Calendar
import calendar
But you do not import the class Calendar from ttkcalendar module (Do not forget to change it as described
here).
So, import should look like this:
import ttkcalendar
import calendar
Creating calendar (I changed the code a bit for clarity):
ttkcal = ttkcalendar.Calendar(root2,firstweekday=calendar.SUNDAY)
In your code, the main window is initialized twice:
line 15: master = Tk()
line 960: root = Tk()
You need to remove the first initialization.
You mix pack() and grid() in the same master window. According the docs, it is a bad idea:
Warning: Never mix grid and pack in the same master window. Tkinter
will happily spend the rest of your lifetime trying to negotiate a
solution that both managers are happy with. Instead of waiting, kill
the application, and take another look at your code. A common mistake
is to use the wrong parent for some of the widgets.
So, instead nb.pack(fill='both', expand='yes') you have to write something like this
nb.grid(column=0, row=0, sticky=(W, E))
Finally, here are links to the fixed code:
ttkcalendar.py (already modified, ready to use): https://gist.github.com/anonymous/5e0d973f57e185572df2
Your script with described modifications:
https://gist.github.com/anonymous/65cb808dc64e414c0c12